Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2002' into 2004
Browse files Browse the repository at this point in the history
# Conflicts:
#	fabric/src/generated/resources/kubejs.accesswidener
#	src/main/java/dev/latvian/mods/kubejs/core/LazyComponentKJS.java
#	src/main/java/dev/latvian/mods/kubejs/core/mixin/common/CommandSourceStackMixin.java
#	src/main/java/dev/latvian/mods/kubejs/item/custom/SmithingTemplateItemBuilder.java
#	src/main/java/dev/latvian/mods/kubejs/neoforge/KubeJSEntryPoint.java
#	src/main/resources/kubejs.accesswidener
  • Loading branch information
MaxNeedsSnacks committed Jan 15, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 1caeef1 + 3117d8c commit 395bb59
Showing 17 changed files with 386 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -63,6 +63,7 @@
import dev.latvian.mods.kubejs.item.custom.RecordItemJS;
import dev.latvian.mods.kubejs.item.custom.ShearsItemBuilder;
import dev.latvian.mods.kubejs.item.custom.ShovelItemBuilder;
import dev.latvian.mods.kubejs.item.custom.SmithingTemplateItemBuilder;
import dev.latvian.mods.kubejs.item.custom.SwordItemBuilder;
import dev.latvian.mods.kubejs.item.ingredient.IngredientJS;
import dev.latvian.mods.kubejs.level.ruletest.KubeJSRuleTests;
@@ -212,6 +213,7 @@ public void init() {
RegistryInfo.ITEM.addType("leggings", ArmorItemBuilder.Leggings.class, ArmorItemBuilder.Leggings::new);
RegistryInfo.ITEM.addType("boots", ArmorItemBuilder.Boots.class, ArmorItemBuilder.Boots::new);
RegistryInfo.ITEM.addType("music_disc", RecordItemJS.Builder.class, RecordItemJS.Builder::new);
RegistryInfo.ITEM.addType("smithing_template", SmithingTemplateItemBuilder.class, SmithingTemplateItemBuilder::new);

RegistryInfo.FLUID.addType("basic", FluidBuilder.class, FluidBuilder::new);
RegistryInfo.ENCHANTMENT.addType("basic", EnchantmentBuilder.class, EnchantmentBuilder::new);
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@
public interface BlockEvents {
EventGroup GROUP = EventGroup.of("BlockEvents");

Extra SUPPORTS_BLOCK = new Extra().transformer(BlockEvents::transformBlock).toString(o -> ((Block) o).kjs$getId()).identity();
Extra SUPPORTS_BLOCK = new Extra().transformer(BlockEvents::transformBlock).toString(o -> ((Block) o).kjs$getId()).identity().describeType(context -> context.javaType(Block.class));

private static Block transformBlock(Object o) {
if (o == null) {
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
public interface EntityEvents {
EventGroup GROUP = EventGroup.of("EntityEvents");

Extra SUPPORTS_ENTITY_TYPE = new Extra().transformer(EntityEvents::transformEntityType).identity();
Extra SUPPORTS_ENTITY_TYPE = new Extra().transformer(EntityEvents::transformEntityType).identity().describeType(context -> context.javaType(EntityType.class));

private static Object transformEntityType(Object o) {
if (o == null || o instanceof EntityType) {
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@
public interface ItemEvents {
EventGroup GROUP = EventGroup.of("ItemEvents");

Extra SUPPORTS_ITEM = new Extra().transformer(ItemEvents::transformItem).toString(o -> ((Item) o).kjs$getId()).identity();
Extra SUPPORTS_ITEM = new Extra().transformer(ItemEvents::transformItem).toString(o -> ((Item) o).kjs$getId()).identity().describeType(context -> context.javaType(Item.class));

private static Object transformItem(Object o) {
if (o == null) {
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
import net.minecraft.world.inventory.MenuType;

public interface PlayerEvents {
Extra SUPPORTS_MENU_TYPE = new Extra().transformer(PlayerEvents::transformMenuType).identity();
Extra SUPPORTS_MENU_TYPE = new Extra().transformer(PlayerEvents::transformMenuType).identity().describeType(context -> context.javaType(MenuType.class));

static Object transformMenuType(Object o) {
if (o == null) {
Original file line number Diff line number Diff line change
@@ -468,7 +468,12 @@ public BlockBuilder item(@Nullable Consumer<BlockItemBuilder> i) {
itemBuilder = null;
lootTable = EMPTY;
} else {
i.accept(getOrCreateItemBuilder());
if (itemBuilder == null) {
itemBuilder = getOrCreateItemBuilder();
itemBuilder.blockBuilder = this;
ScriptType.STARTUP.console.warn("`item` is called with non-null builder callback after block item is set to null! Creating another block item as fallback.");
}
i.accept(itemBuilder);
}

return this;
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@

public class BlockEntityJS extends BlockEntity {
public final BlockEntityInfo info;
public final BlockContainerJS block;
protected BlockContainerJS block;
public final int x, y, z;
public int tick, cycle;
public CompoundTag data;
@@ -29,9 +29,6 @@ public class BlockEntityJS extends BlockEntity {
public BlockEntityJS(BlockPos blockPos, BlockState blockState, BlockEntityInfo entityInfo) {
super(entityInfo.entityType, blockPos, blockState);
this.info = entityInfo;
this.block = new BlockContainerJS(level, blockPos);
this.block.cachedState = blockState;
this.block.cachedEntity = this;
this.x = blockPos.getX();
this.y = blockPos.getY();
this.z = blockPos.getZ();
@@ -169,4 +166,12 @@ public void postTick(boolean c) {
cycle++;
}
}

public BlockContainerJS getBlock() {
if (block == null) {
this.block = new BlockContainerJS(level, worldPosition);
this.block.cachedEntity = this;
}
return block;
}
}
10 changes: 10 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/core/LazyComponentKJS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.latvian.mods.kubejs.core;

import net.minecraft.network.chat.Component;

import java.util.function.Supplier;

@FunctionalInterface
public interface LazyComponentKJS extends Supplier<Component> {
Component get();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dev.latvian.mods.kubejs.core.mixin.common;

import dev.latvian.mods.kubejs.core.LazyComponentKJS;
import dev.latvian.mods.rhino.util.HideFromJS;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;

import java.util.function.Supplier;

@Mixin(CommandSourceStack.class)
@RemapPrefixForJS("kjs$")
public abstract class CommandSourceStackMixin {
@Shadow
@HideFromJS
public abstract void sendSuccess(Supplier<Component> supplier, boolean bl);

@Unique
public void kjs$sendSuccess(Component component, boolean broadcastToAdmins) {
kjs$sendSuccessLazy(() -> component, broadcastToAdmins);
}

@Unique
public void kjs$sendSuccessLazy(LazyComponentKJS component, boolean broadcastToAdmins) {
sendSuccess(component, broadcastToAdmins);
}
}
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.level.BaseSpawner;
import net.minecraft.world.level.Level;

@Info("""
@@ -18,14 +19,16 @@ public class CheckLivingEntitySpawnEventJS extends LivingEntityEventJS {

public final double x, y, z;
public final MobSpawnType type;
public final BaseSpawner spawner;

public CheckLivingEntitySpawnEventJS(LivingEntity entity, Level level, double x, double y, double z, MobSpawnType type) {
public CheckLivingEntitySpawnEventJS(LivingEntity entity, Level level, double x, double y, double z, MobSpawnType type, BaseSpawner spawner) {
this.entity = entity;
this.level = level;
this.x = x;
this.y = y;
this.z = z;
this.type = type;
this.spawner = spawner;
}

@Override
@@ -49,4 +52,9 @@ public BlockContainerJS getBlock() {
public MobSpawnType getType() {
return type;
}

@Info("The spawner that spawned the entity.")
public BaseSpawner getSpawner() {
return spawner;
}
}
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ public static void init() {

private static EventResult checkSpawn(LivingEntity entity, LevelAccessor la, double x, double y, double z, MobSpawnType type, BaseSpawner spawner) {
if (la instanceof Level level && (la.isClientSide() || UtilsJS.staticServer != null) && EntityEvents.CHECK_SPAWN.hasListeners()) {
return EntityEvents.CHECK_SPAWN.post(level, entity.getType(), new CheckLivingEntitySpawnEventJS(entity, level, x, y, z, type)).arch();
return EntityEvents.CHECK_SPAWN.post(level, entity.getType(), new CheckLivingEntitySpawnEventJS(entity, level, x, y, z, type, spawner)).arch();
}

return EventResult.pass();
10 changes: 10 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/event/Extra.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package dev.latvian.mods.kubejs.event;

import dev.latvian.mods.kubejs.typings.desc.DescriptionContext;
import dev.latvian.mods.kubejs.typings.desc.TypeDescJS;
import dev.latvian.mods.kubejs.util.UtilsJS;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

import java.util.function.Function;
import java.util.function.Predicate;

public class Extra {
@@ -62,13 +65,15 @@ private static ResourceKey<? extends Registry<?>> toRegistryKey(Object object) {
public boolean required;
public Predicate<Object> validator;
public Transformer toString;
public Function<DescriptionContext, TypeDescJS> describeType;

public Extra() {
this.transformer = Transformer.IDENTITY;
this.identity = false;
this.required = false;
this.validator = UtilsJS.ALWAYS_TRUE;
this.toString = Transformer.IDENTITY;
this.describeType = context -> TypeDescJS.STRING;
}

public Extra copy() {
@@ -101,6 +106,11 @@ public Extra validator(Predicate<Object> validator) {
return this;
}

public Extra describeType(Function<DescriptionContext, TypeDescJS> describeType) {
this.describeType = describeType;
return this;
}

public Extra toString(Transformer factory) {
this.toString = factory;
return this;
Original file line number Diff line number Diff line change
@@ -61,8 +61,7 @@ public void groupFluidsByTag(ResourceLocation groupId, Component description, Re
}

// predicate-based grouping, again with shortcuts for builtin entry types
public void groupItemsIf(ResourceLocation groupId, Component description, Ingredient predicate) {
// Ingredient is already a predicate for ItemStack, so just use testVanilla
public void groupItemsIf(ResourceLocation groupId, Component description, Predicate<ItemStack> predicate) {
registry.group(groupId, description, VanillaEntryTypes.ITEM, (item) -> predicate.test(item.getValue()));
}

Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ public HandheldItemBuilder(ResourceLocation i, float d, float s) {
}

public HandheldItemBuilder tier(Tier t) {
toolTier = new MutableToolTier(t);
toolTier = t instanceof MutableToolTier mtt ? mtt : new MutableToolTier(t);
return this;
}

Loading

0 comments on commit 395bb59

Please sign in to comment.