Skip to content

Commit

Permalink
Fixed conditional tag recipes not working.. again. Renamed BlockEntit…
Browse files Browse the repository at this point in the history
…yJS to KubeBlockEntity
  • Loading branch information
LatvianModder committed Jul 30, 2024
1 parent 797bddd commit 36d94d6
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 23 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ modrinth_id=umyGl7zF
minecraft_version=1.21
mod_version=2100.7.0

neoforge_version=21.0.139-beta
neoforge_version=21.0.145
parchment_version=2024.07.07
rhino_version=2100.2.5-build.44

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import dev.latvian.mods.kubejs.block.callbacks.CanBeReplacedCallbackJS;
import dev.latvian.mods.kubejs.block.callbacks.EntityFallenOnBlockCallbackJS;
import dev.latvian.mods.kubejs.block.callbacks.EntitySteppedOnBlockCallbackJS;
import dev.latvian.mods.kubejs.block.entity.BlockEntityJS;
import dev.latvian.mods.kubejs.block.entity.KubeBlockEntity;
import dev.latvian.mods.kubejs.level.BlockContainerJS;
import dev.latvian.mods.kubejs.script.ScriptType;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -338,7 +338,7 @@ public ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Level
@Override
public void onRemove(BlockState state, Level level, BlockPos pos, BlockState newState, boolean bl) {
if (!state.is(newState.getBlock())) {
if (level.getBlockEntity(pos) instanceof BlockEntityJS entity) {
if (level.getBlockEntity(pos) instanceof KubeBlockEntity entity) {
if (level instanceof ServerLevel) {
for (var attachment : entity.attachments) {
attachment.onRemove(newState);
Expand All @@ -354,7 +354,7 @@ public void onRemove(BlockState state, Level level, BlockPos pos, BlockState new

@Override
public void setPlacedBy(Level level, BlockPos blockPos, BlockState blockState, @Nullable LivingEntity livingEntity, ItemStack itemStack) {
if (livingEntity != null && !level.isClientSide() && level.getBlockEntity(blockPos) instanceof BlockEntityJS e) {
if (livingEntity != null && !level.isClientSide() && level.getBlockEntity(blockPos) instanceof KubeBlockEntity e) {
e.placerId = livingEntity.getUUID();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface BlockEntityAttachment {
BlockEntityAttachment[] EMPTY_ARRAY = new BlockEntityAttachment[0];

interface Factory {
BlockEntityAttachment create(BlockEntityJS entity);
BlockEntityAttachment create(KubeBlockEntity entity);
}

interface FactoryProvider {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package dev.latvian.mods.kubejs.block.entity;

public interface BlockEntityEventCallback {
void accept(BlockEntityJS entity, int data);
void accept(KubeBlockEntity entity, int data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,23 @@ public void eventHandler(int eventId, BlockEntityEventCallback callback) {

public void rightClickOpensInventory() {
blockBuilder.rightClick = e -> {
if (e.getBlock().getEntity() instanceof BlockEntityJS entity && entity.inventory != null) {
if (e.getBlock().getEntity() instanceof KubeBlockEntity entity && entity.inventory != null) {
((ServerPlayerKJS) e.getPlayer()).kjs$openInventoryGUI(entity.inventory, blockBuilder.get().getName());
}
};
}

@HideFromJS
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new BlockEntityJS(pos, state, this);
return new KubeBlockEntity(pos, state, this);
}

@HideFromJS
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level) {
if (level.isClientSide()) {
return clientTicking ? (BlockEntityTicker) BlockEntityJS.TICKER : null;
return clientTicking ? (BlockEntityTicker) KubeBlockEntity.TICKER : null;
} else {
return serverTicking ? (BlockEntityTicker) BlockEntityJS.TICKER : null;
return serverTicking ? (BlockEntityTicker) KubeBlockEntity.TICKER : null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import net.minecraft.world.level.Level;

public class BlockEntityTickKubeEvent implements KubeLevelEvent {
private final BlockEntityJS entity;
private final KubeBlockEntity entity;

public BlockEntityTickKubeEvent(BlockEntityJS entity) {
public BlockEntityTickKubeEvent(KubeBlockEntity entity) {
this.entity = entity;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ public class InventoryAttachment extends SimpleContainer implements BlockEntityA

private record InventoryAttachmentFactory(int width, int height, @Nullable ItemPredicate inputFilter) implements Factory {
@Override
public BlockEntityAttachment create(BlockEntityJS entity) {
public BlockEntityAttachment create(KubeBlockEntity entity) {
return new InventoryAttachment(entity, width, height, inputFilter);
}
}

public final int width, height;
public final BlockEntityJS blockEntity;
public final KubeBlockEntity blockEntity;
public final ItemPredicate inputFilter;

public InventoryAttachment(BlockEntityJS blockEntity, int width, int height, @Nullable ItemPredicate inputFilter) {
public InventoryAttachment(KubeBlockEntity blockEntity, int width, int height, @Nullable ItemPredicate inputFilter) {
super(width * height);
this.width = width;
this.height = height;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

import java.util.UUID;

public class BlockEntityJS extends BlockEntity {
public static final BlockEntityTicker<BlockEntityJS> TICKER = (level, pos, state, entity) -> entity.tick();
public class KubeBlockEntity extends BlockEntity {
public static final BlockEntityTicker<KubeBlockEntity> TICKER = (level, pos, state, entity) -> entity.tick();

public final BlockEntityInfo info;
public final ResourceKey<Block> blockKey;
Expand All @@ -35,7 +35,7 @@ public class BlockEntityJS extends BlockEntity {
public UUID placerId;
private BlockEntityTickKubeEvent tickEvent;

public BlockEntityJS(BlockPos blockPos, BlockState blockState, BlockEntityInfo entityInfo) {
public KubeBlockEntity(BlockPos blockPos, BlockState blockState, BlockEntityInfo entityInfo) {
super(entityInfo.entityType, blockPos, blockState);
this.info = entityInfo;
this.blockKey = blockState.kjs$getKey();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.latvian.mods.kubejs.core.mixin;

import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.neoforged.neoforge.common.conditions.ICondition;
import net.neoforged.neoforge.common.conditions.TagEmptyCondition;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(TagEmptyCondition.class)
public abstract class TagEmptyConditionMixin {
@Shadow
@Final
private TagKey<Item> tag;

@Inject(method = "test", at = @At("HEAD"), cancellable = true, remap = false)
private void kjs$test(ICondition.IContext ctx, CallbackInfoReturnable<Boolean> cir) {
if (ctx instanceof RegistryAccessContainer c && c.cachedItemTags != null) {
cir.setReturnValue(c.cachedItemTags.isEmpty(tag));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void visitOptionalDependencies(Consumer<ResourceLocation> visitor) {

public final Registry<T> registry;
public final Map<ResourceLocation, List<TagLoader.EntryWithSource>> originalMap;
private Map<ResourceLocation, Collection<Holder<T>>> tagMap;
private Map<TagKey<T>, Set<T>> keyToValue;
private Map<T, Set<TagKey<T>>> valueToKey;

Expand Down Expand Up @@ -88,7 +89,7 @@ public Collection<T> tag(ResourceLocation id) {
return map;
}

private Map<TagKey<T>, Set<T>> keyToValue() {
public Map<TagKey<T>, Set<T>> keyToValue() {
if (keyToValue == null) {
var map = build(originalMap);
keyToValue = new Reference2ObjectOpenHashMap<>(map.size());
Expand Down Expand Up @@ -143,4 +144,25 @@ public Map<TagKey<T>, List<Holder<T>>> bindingMap() {

return map;
}

public Map<ResourceLocation, Collection<Holder<T>>> tagMap() {
if (tagMap == null) {
tagMap = new HashMap<>();
var k2v = keyToValue();

for (var entry : k2v.entrySet()) {
var list = new ArrayList<Holder<T>>(entry.getValue().size());

for (var value : entry.getValue()) {
list.add(registry.wrapAsHolder(value));
}

tagMap.put(entry.getKey().location(), list);
}

tagMap = Map.copyOf(tagMap);
}

return tagMap;
}
}
4 changes: 2 additions & 2 deletions src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -511,15 +511,15 @@ public RecipeHolder<?> createRecipe() {
return new RecipeHolder<>(getOrCreateId(), originalRecipe.getValue());
}

return RecipeHelper.fromJson(type.event.registries.json(), getSerializationTypeFunction().schemaType.getSerializer(), getOrCreateId(), json, DevProperties.get().logErroringParsedRecipes);
return RecipeHelper.fromJson(type.event.jsonOps, getSerializationTypeFunction().schemaType.getSerializer(), getOrCreateId(), json, DevProperties.get().logErroringParsedRecipes);
}

@Nullable
public Recipe<?> getOriginalRecipe() {
if (originalRecipe == null) {
originalRecipe = new MutableObject<>();
try {
var holder = RecipeHelper.fromJson(type.event.registries.json(), type.schemaType.getSerializer(), getOrCreateId(), originalJson, DevProperties.get().logErroringParsedRecipes);
var holder = RecipeHelper.fromJson(type.event.jsonOps, type.schemaType.getSerializer(), getOrCreateId(), originalJson, DevProperties.get().logErroringParsedRecipes);

if (holder != null) {
originalRecipe.setValue(holder.value());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
import dev.latvian.mods.rhino.util.HideFromJS;
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.neoforged.neoforge.common.conditions.ConditionalOps;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
Expand Down Expand Up @@ -120,6 +122,7 @@ private String recipeToString(Recipe<?> recipe) {

public final RecipeSchemaStorage recipeSchemaStorage;
public final RegistryAccessContainer registries;
public final RegistryOps<JsonElement> jsonOps;
public final Map<ResourceLocation, KubeRecipe> originalRecipes;
public final Collection<KubeRecipe> addedRecipes;
private final BinaryOperator<RecipeHolder<?>> mergeOriginal, mergeAdded;
Expand All @@ -146,6 +149,7 @@ public RecipesKubeEvent(ServerScriptManager manager) {
ConsoleJS.SERVER.info("Initializing recipe event...");
this.recipeSchemaStorage = manager.recipeSchemaStorage;
this.registries = manager.getRegistries();
this.jsonOps = new ConditionalOps<>(registries.json(), registries);
this.originalRecipes = new HashMap<>();
this.addedRecipes = new ConcurrentLinkedQueue<>();
this.recipeFunctions = new HashMap<>();
Expand Down Expand Up @@ -235,7 +239,7 @@ public void post(RecipeManagerKJS recipeManager, Map<ResourceLocation, JsonEleme
continue; //Forge: filter anything beginning with "_" as it's used for metadata.
}

var jsonResult = RecipeHelper.validate(registries.json(), entry.getValue());
var jsonResult = RecipeHelper.validate(jsonOps, entry.getValue());

if (jsonResult.error().isPresent()) {
var error = jsonResult.error().get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import dev.latvian.mods.kubejs.server.DataExport;
import dev.latvian.mods.rhino.Context;
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
Expand All @@ -28,14 +29,16 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.neoforge.common.conditions.ICondition;
import org.jetbrains.annotations.ApiStatus;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class RegistryAccessContainer {
public final class RegistryAccessContainer implements ICondition.IContext {
public static final RegistryAccessContainer BUILTIN = new RegistryAccessContainer(RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY));

// Still necessary because STARTUP and CLIENT scripts need to know about registries
Expand Down Expand Up @@ -187,4 +190,15 @@ public RegistryWrapper wrapRegistry(ResourceLocation id) {

return cachedRegistryWrappers.computeIfAbsent(id, this::createRegistryWrapper);
}

@Override
public <T> Map<ResourceLocation, Collection<Holder<T>>> getAllTags(ResourceKey<? extends Registry<T>> key) {
var cached = cachedRegistryTags.get(key);

if (cached != null) {
return (Map) cached.lookup().tagMap();
}

return Map.of();
}
}
1 change: 1 addition & 0 deletions src/main/resources/kubejs.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"SizedIngredientMixin",
"StringRepresentableMixin",
"StringTagMixin",
"TagEmptyConditionMixin",
"TagLoaderMixin",
"TagManagerMixin",
"TextColorMixin",
Expand Down

0 comments on commit 36d94d6

Please sign in to comment.