From 811736a411fe640983c3f57bf783119b3a5736e0 Mon Sep 17 00:00:00 2001 From: BlayTheNinth <1933180+BlayTheNinth@users.noreply.github.com> Date: Fri, 1 Nov 2024 17:12:54 +0100 Subject: [PATCH] wip: Continue port to Minecraft 1.21.3 --- .../entity/AutoCompressorBlockEntity.java | 24 +++++++-------- .../RationingAutoCompressorBlockEntity.java | 2 +- .../handler/HammerSpeedHandler.java | 16 ++-------- .../excompressum/item/OreSmasherItem.java | 8 ++--- .../registry/compressor/CompressedRecipe.java | 24 ++------------- .../compressor/CompressedRecipeRegistry.java | 29 +++++++++++-------- 6 files changed, 38 insertions(+), 65 deletions(-) diff --git a/common/src/main/java/net/blay09/mods/excompressum/block/entity/AutoCompressorBlockEntity.java b/common/src/main/java/net/blay09/mods/excompressum/block/entity/AutoCompressorBlockEntity.java index 07589a0c..002b6494 100644 --- a/common/src/main/java/net/blay09/mods/excompressum/block/entity/AutoCompressorBlockEntity.java +++ b/common/src/main/java/net/blay09/mods/excompressum/block/entity/AutoCompressorBlockEntity.java @@ -25,6 +25,7 @@ import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.chat.Component; import net.minecraft.network.codec.StreamCodec; +import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.Container; import net.minecraft.world.entity.player.Inventory; @@ -129,7 +130,7 @@ public AutoCompressorBlockEntity(BlockEntityType type, BlockPos pos, BlockSta } public boolean shouldCompress(Multiset inputItems, CompressedRecipe compressedRecipe) { - return inputItems.count(compressedRecipe) >= compressedRecipe.getCount(); + return inputItems.count(compressedRecipe) >= compressedRecipe.count(); } public static void serverTick(Level level, BlockPos pos, BlockState state, AutoCompressorBlockEntity blockEntity) { @@ -151,24 +152,24 @@ public void serverTick() { } } for (CompressedRecipe compressedRecipe : inputItems.elementSet()) { - Ingredient ingredient = compressedRecipe.getIngredient(); + Ingredient ingredient = compressedRecipe.ingredient(); if (shouldCompress(inputItems, compressedRecipe)) { int space = 0; for (int i = 0; i < outputSlots.getContainerSize(); i++) { ItemStack slotStack = outputSlots.getItem(i); if (slotStack.isEmpty()) { space = 64; - } else if (isItemEqualWildcard(slotStack, compressedRecipe.getResultStack())) { + } else if (isItemEqualWildcard(slotStack, compressedRecipe.resultStack())) { space += slotStack.getMaxStackSize() - slotStack.getCount(); } - if (space >= compressedRecipe.getResultStack().getCount()) { + if (space >= compressedRecipe.resultStack().getCount()) { break; } } - if (space < compressedRecipe.getResultStack().getCount()) { + if (space < compressedRecipe.resultStack().getCount()) { continue; } - int count = compressedRecipe.getCount(); + int count = compressedRecipe.count(); for (int i = 0; i < inputSlots.getContainerSize(); i++) { ItemStack slotStack = inputSlots.getItem(i); if (!slotStack.isEmpty() && ingredient.test(slotStack)) { @@ -200,7 +201,7 @@ public void serverTick() { if (!level.isClientSide) { CompressedRecipe compressedRecipe = currentRecipe; if (compressedRecipe != null) { - ItemStack resultStack = compressedRecipe.getResultStack().copy(); + ItemStack resultStack = compressedRecipe.resultStack().copy(); if (!addItemToOutput(resultStack)) { overflowBuffer.add(resultStack); } @@ -255,11 +256,8 @@ public float getEffectiveSpeed() { @Override public void loadAdditional(CompoundTag tagCompound, HolderLookup.Provider provider) { - if (tagCompound.contains("CurrentRecipeResult")) { - ItemStack itemStack = ItemStack.parseOptional(provider, tagCompound.getCompound("CurrentRecipeResult")); - if (!itemStack.isEmpty()) { - // TODO currentRecipe = new CompressedRecipe(Ingredient.EMPTY, 0, itemStack); - } + if (tagCompound.contains("CurrentRecipe")) { + currentRecipe = ExRegistries.getCompressedRecipeRegistry().getRecipeById(ResourceLocation.parse(tagCompound.getString("CurrentRecipe"))); } isDisabledByRedstone = tagCompound.getBoolean("IsDisabledByRedstone"); progress = tagCompound.getFloat("Progress"); @@ -274,7 +272,7 @@ public void loadAdditional(CompoundTag tagCompound, HolderLookup.Provider provid @Override public void saveAdditional(CompoundTag tag, HolderLookup.Provider provider) { if (currentRecipe != null) { - tag.put("CurrentRecipeResult", currentRecipe.getResultStack().save(provider)); + tag.putString("CurrentRecipe", currentRecipe.id().toString()); } tag.putBoolean("IsDisabledByRedstone", isDisabledByRedstone); tag.putFloat("Progress", progress); diff --git a/common/src/main/java/net/blay09/mods/excompressum/block/entity/RationingAutoCompressorBlockEntity.java b/common/src/main/java/net/blay09/mods/excompressum/block/entity/RationingAutoCompressorBlockEntity.java index 77ddf765..22db3b88 100644 --- a/common/src/main/java/net/blay09/mods/excompressum/block/entity/RationingAutoCompressorBlockEntity.java +++ b/common/src/main/java/net/blay09/mods/excompressum/block/entity/RationingAutoCompressorBlockEntity.java @@ -12,6 +12,6 @@ public RationingAutoCompressorBlockEntity(BlockPos pos, BlockState state) { @Override public boolean shouldCompress(Multiset inputItems, CompressedRecipe compressedRecipe) { - return inputItems.count(compressedRecipe) >= compressedRecipe.getCount() + 1; + return inputItems.count(compressedRecipe) >= compressedRecipe.count() + 1; } } diff --git a/common/src/main/java/net/blay09/mods/excompressum/handler/HammerSpeedHandler.java b/common/src/main/java/net/blay09/mods/excompressum/handler/HammerSpeedHandler.java index 55df2573..8f851c67 100644 --- a/common/src/main/java/net/blay09/mods/excompressum/handler/HammerSpeedHandler.java +++ b/common/src/main/java/net/blay09/mods/excompressum/handler/HammerSpeedHandler.java @@ -2,13 +2,10 @@ import net.blay09.mods.balm.api.Balm; import net.blay09.mods.balm.api.event.DigSpeedEvent; -import net.blay09.mods.excompressum.registry.ExRegistries; +import net.blay09.mods.excompressum.tag.ModBlockTags; import net.blay09.mods.excompressum.tag.ModItemTags; -import net.blay09.mods.excompressum.utils.StupidUtils; import net.minecraft.core.component.DataComponents; -import net.minecraft.server.level.ServerLevel; import net.minecraft.world.InteractionHand; -import net.minecraft.world.item.ItemStack; public class HammerSpeedHandler { @@ -17,15 +14,8 @@ public static void initialize() { } public static void onDigSpeed(DigSpeedEvent event) { - ItemStack heldItem = event.getPlayer().getItemInHand(InteractionHand.MAIN_HAND); - final var targetItem = StupidUtils.getItemStackFromState(event.getState()); - final var level = event.getPlayer().level(); - if (!(level instanceof ServerLevel serverLevel)) { - return; // TODO I believe this needs to run on client too though... might have to upgrade more stuff to tags and to be less dependent on recipe existence - } - - if ((heldItem.is(ModItemTags.HAMMERS) || heldItem.is(ModItemTags.COMPRESSED_HAMMERS)) && (ExRegistries.getHammerRegistry() - .isHammerable(serverLevel, targetItem) || ExRegistries.getCompressedHammerRegistry().isHammerable(serverLevel, targetItem))) { + final var heldItem = event.getPlayer().getItemInHand(InteractionHand.MAIN_HAND); + if ((heldItem.is(ModItemTags.HAMMERS) || heldItem.is(ModItemTags.COMPRESSED_HAMMERS)) && event.getState().is(ModBlockTags.MINEABLE_WITH_HAMMER)) { float newSpeed = 2f; final var tool = heldItem.get(DataComponents.TOOL); if (tool != null) { diff --git a/common/src/main/java/net/blay09/mods/excompressum/item/OreSmasherItem.java b/common/src/main/java/net/blay09/mods/excompressum/item/OreSmasherItem.java index 1c66a747..b9850eca 100644 --- a/common/src/main/java/net/blay09/mods/excompressum/item/OreSmasherItem.java +++ b/common/src/main/java/net/blay09/mods/excompressum/item/OreSmasherItem.java @@ -59,14 +59,14 @@ public InteractionResult useOn(UseOnContext context) { if (!inventoryStack.isEmpty()) { if (ExCompressumAPI.getExNihilo().isCompressableOre(inventoryStack)) { CompressedRecipe recipe = ExRegistries.getCompressedRecipeRegistry().getRecipe(inventoryStack); - if (recipe != null && recipe.getResultStack().getCount() == 1) { - if (inventoryStack.getCount() >= recipe.getCount()) { + if (recipe != null && recipe.resultStack().getCount() == 1) { + if (inventoryStack.getCount() >= recipe.count()) { BlockState oldState = level.getBlockState(pos); - ItemStack resultStack = recipe.getResultStack().copy(); + ItemStack resultStack = recipe.resultStack().copy(); resultStack.getItem().useOn(new UseOnContext(player, context.getHand(), new BlockHitResult(context.getClickLocation(), context.getClickedFace(), context.getClickedPos(), context.isInside()))); level.sendBlockUpdated(pos, oldState, level.getBlockState(pos), 3); if (resultStack.isEmpty()) { - inventoryStack.shrink(recipe.getCount()); + inventoryStack.shrink(recipe.count()); if (inventoryStack.isEmpty()) { player.getInventory().items.remove(i); } diff --git a/common/src/main/java/net/blay09/mods/excompressum/registry/compressor/CompressedRecipe.java b/common/src/main/java/net/blay09/mods/excompressum/registry/compressor/CompressedRecipe.java index 0bf78009..6ad02b4c 100644 --- a/common/src/main/java/net/blay09/mods/excompressum/registry/compressor/CompressedRecipe.java +++ b/common/src/main/java/net/blay09/mods/excompressum/registry/compressor/CompressedRecipe.java @@ -1,28 +1,8 @@ package net.blay09.mods.excompressum.registry.compressor; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.Ingredient; -public class CompressedRecipe { - private final Ingredient ingredient; - private final int count; - private final ItemStack resultStack; - - public CompressedRecipe(Ingredient ingredient, int count, ItemStack resultStack) { - this.ingredient = ingredient; - this.count = count; - this.resultStack = resultStack; - } - - public Ingredient getIngredient() { - return ingredient; - } - - public int getCount() { - return count; - } - - public ItemStack getResultStack() { - return resultStack; - } +public record CompressedRecipe(ResourceLocation id, Ingredient ingredient, int count, ItemStack resultStack) { } diff --git a/common/src/main/java/net/blay09/mods/excompressum/registry/compressor/CompressedRecipeRegistry.java b/common/src/main/java/net/blay09/mods/excompressum/registry/compressor/CompressedRecipeRegistry.java index c2415d53..4fa4a707 100644 --- a/common/src/main/java/net/blay09/mods/excompressum/registry/compressor/CompressedRecipeRegistry.java +++ b/common/src/main/java/net/blay09/mods/excompressum/registry/compressor/CompressedRecipeRegistry.java @@ -18,16 +18,18 @@ public class CompressedRecipeRegistry { + private final Map recipesById = new HashMap<>(); private final List recipesSmall = new ArrayList<>(); private final List recipes = new ArrayList<>(); - private final Map cachedResults = new HashMap<>(); + private final Map cacheByItemId = new HashMap<>(); public CompressedRecipeRegistry() { } public void reloadRecipes(RecipeManager recipeManager, RegistryAccess registryAccess) { - cachedResults.clear(); + recipesById.clear(); + cacheByItemId.clear(); recipesSmall.clear(); recipes.clear(); @@ -66,11 +68,11 @@ public void reloadRecipes(RecipeManager recipeManager, RegistryAccess registryAc final var result = ((ShapedRecipeAccessor) shapedRecipe).getResult(); if (count == 4 && shapedRecipe.getWidth() == 2 && shapedRecipe.getHeight() == 2) { if (passes) { - recipesSmall.add(new CompressedRecipe(first, 4, result.copy())); + recipesSmall.add(new CompressedRecipe(recipeHolder.id().location(), first, 4, result.copy())); } } else if (count == 9 && shapedRecipe.getWidth() == 3 && shapedRecipe.getHeight() == 3) { if (passes) { - recipes.add(new CompressedRecipe(first, 9, result.copy())); + recipes.add(new CompressedRecipe(recipeHolder.id().location(), first, 9, result.copy())); } } } @@ -97,11 +99,11 @@ public void reloadRecipes(RecipeManager recipeManager, RegistryAccess registryAc final var result = ((ShapelessRecipeAccessor) shapelessRecipe).getResult(); if (count == 4) { if (passes) { - recipesSmall.add(new CompressedRecipe(first, 4, result.copy())); + recipesSmall.add(new CompressedRecipe(recipeHolder.id().location(), first, 4, result.copy())); } } else { if (passes) { - recipes.add(new CompressedRecipe(first, 9, result.copy())); + recipes.add(new CompressedRecipe(recipeHolder.id().location(), first, 9, result.copy())); } } } @@ -116,27 +118,30 @@ public CompressedRecipe getRecipe(ItemStack itemStack) { } final ResourceLocation registryName = Balm.getRegistries().getKey(itemStack.getItem()); - CompressedRecipe foundRecipe = cachedResults.get(registryName); + CompressedRecipe foundRecipe = cacheByItemId.get(registryName); if (foundRecipe != null) { return foundRecipe; } for (CompressedRecipe recipe : recipes) { - if (recipe.getIngredient().test(itemStack)) { - cachedResults.put(registryName, recipe); + if (recipe.ingredient().test(itemStack)) { + cacheByItemId.put(registryName, recipe); return recipe; } } for (CompressedRecipe recipe : recipesSmall) { - if (recipe.getIngredient().test(itemStack)) { - cachedResults.put(registryName, recipe); + if (recipe.ingredient().test(itemStack)) { + cacheByItemId.put(registryName, recipe); return recipe; } } - cachedResults.put(registryName, null); + cacheByItemId.put(registryName, null); return null; } + public CompressedRecipe getRecipeById(ResourceLocation id) { + return recipesById.get(id); + } }