From 7e367a1971ee93173bba97fc3f6f20004a2d099b Mon Sep 17 00:00:00 2001 From: Aleksey Date: Sat, 13 Feb 2021 00:15:11 +0300 Subject: [PATCH] Anvil crafting fixes --- .../ru/betterend/blocks/AeterniumAnvil.java | 47 ++++--------------- .../betterend/blocks/basis/EndAnvilBlock.java | 10 ++-- .../ru/betterend/config/ConfigKeeper.java | 3 +- .../AnvilScreenHandlerExtended.java | 20 ++++---- .../mixin/common/AnvilBlockMixin.java | 15 ++++-- .../mixin/common/AnvilScreenHandlerMixin.java | 42 ++++++++++------- .../ru/betterend/recipe/AnvilRecipes.java | 13 ++--- .../recipe/builders/AnvilRecipe.java | 7 ++- .../java/ru/betterend/registry/EndTags.java | 2 + 9 files changed, 76 insertions(+), 83 deletions(-) diff --git a/src/main/java/ru/betterend/blocks/AeterniumAnvil.java b/src/main/java/ru/betterend/blocks/AeterniumAnvil.java index c0268a3b..c66b29eb 100644 --- a/src/main/java/ru/betterend/blocks/AeterniumAnvil.java +++ b/src/main/java/ru/betterend/blocks/AeterniumAnvil.java @@ -8,68 +8,37 @@ import java.util.Map; import com.google.common.collect.Maps; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; -import net.minecraft.block.AnvilBlock; -import net.minecraft.block.Block; -import net.minecraft.block.BlockState; -import net.minecraft.block.Blocks; +import net.minecraft.block.*; import net.minecraft.item.ItemStack; import net.minecraft.loot.context.LootContext; import net.minecraft.state.StateManager; import net.minecraft.state.property.IntProperty; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; +import ru.betterend.blocks.basis.EndAnvilBlock; import ru.betterend.item.material.EndToolMaterial; import ru.betterend.patterns.BlockPatterned; import ru.betterend.patterns.Patterns; import ru.betterend.registry.EndBlocks; -public class AeterniumAnvil extends AnvilBlock implements BlockPatterned { +public class AeterniumAnvil extends EndAnvilBlock { public static final IntProperty DESTRUCTION = BlockProperties.DESTRUCTION_LONG; - private final int level; public AeterniumAnvil() { - super(FabricBlockSettings.copyOf(Blocks.ANVIL).materialColor(EndBlocks.AETERNIUM_BLOCK.getDefaultMaterialColor())); - this.level = EndToolMaterial.AETERNIUM.getMiningLevel(); + super(EndBlocks.AETERNIUM_BLOCK.getDefaultMaterialColor(), EndToolMaterial.AETERNIUM.getMiningLevel()); } @Override protected void appendProperties(StateManager.Builder builder) { - super.appendProperties(builder); builder.add(DESTRUCTION); + builder.add(FACING); } - public int getCraftingLevel() { - return level; - } - @Override - public List getDroppedStacks(BlockState state, LootContext.Builder builder) { - return Collections.singletonList(new ItemStack(this)); + public IntProperty getDestructionProperty() { + return DESTRUCTION; } - - @Override - public String getStatesPattern(Reader data) { - Identifier blockId = Registry.BLOCK.getId(this); - return Patterns.createJson(data, blockId.getPath(), blockId.getPath()); - } - - @Override - public String getModelPattern(String block) { - Identifier blockId = Registry.BLOCK.getId(this); - Map map = Maps.newHashMap(); - map.put("%anvil%", blockId.getPath()); - map.put("%top%", getTop(blockId, block)); - return Patterns.createJson(Patterns.BLOCK_ANVIL, map); - } - - private String getTop(Identifier blockId, String block) { - if (block.contains("item")) { - return blockId.getPath() + "_top_0"; - } - char last = block.charAt(block.length() - 1); - return blockId.getPath() + "_top_" + last; - } - + @Override public Identifier statePatternId() { return Patterns.STATE_ANVIL_LONG; diff --git a/src/main/java/ru/betterend/blocks/basis/EndAnvilBlock.java b/src/main/java/ru/betterend/blocks/basis/EndAnvilBlock.java index b63cd2d0..f56221fa 100644 --- a/src/main/java/ru/betterend/blocks/basis/EndAnvilBlock.java +++ b/src/main/java/ru/betterend/blocks/basis/EndAnvilBlock.java @@ -25,7 +25,7 @@ import ru.betterend.patterns.Patterns; public class EndAnvilBlock extends AnvilBlock implements BlockPatterned { public static final IntProperty DESTRUCTION = BlockProperties.DESTRUCTION; - private final int level; + protected final int level; public EndAnvilBlock(MaterialColor color, int level) { super(FabricBlockSettings.copyOf(Blocks.ANVIL).materialColor(color)); @@ -38,6 +38,10 @@ public class EndAnvilBlock extends AnvilBlock implements BlockPatterned { builder.add(DESTRUCTION); } + public IntProperty getDestructionProperty() { + return DESTRUCTION; + } + public int getCraftingLevel() { return level; } @@ -61,8 +65,8 @@ public class EndAnvilBlock extends AnvilBlock implements BlockPatterned { map.put("%top%", getTop(blockId, block)); return Patterns.createJson(Patterns.BLOCK_ANVIL, map); } - - private String getTop(Identifier blockId, String block) { + + protected String getTop(Identifier blockId, String block) { if (block.contains("item")) { return blockId.getPath() + "_top_0"; } diff --git a/src/main/java/ru/betterend/config/ConfigKeeper.java b/src/main/java/ru/betterend/config/ConfigKeeper.java index 5c951bd6..84133efc 100644 --- a/src/main/java/ru/betterend/config/ConfigKeeper.java +++ b/src/main/java/ru/betterend/config/ConfigKeeper.java @@ -17,8 +17,7 @@ import ru.betterend.util.JsonFactory; public final class ConfigKeeper { - private Map> configEntries = Maps.newHashMap(); - + private final Map> configEntries = Maps.newHashMap(); private final JsonObject configObject; private final ConfigWriter writer; diff --git a/src/main/java/ru/betterend/interfaces/AnvilScreenHandlerExtended.java b/src/main/java/ru/betterend/interfaces/AnvilScreenHandlerExtended.java index 37c04cf2..9146ab8e 100644 --- a/src/main/java/ru/betterend/interfaces/AnvilScreenHandlerExtended.java +++ b/src/main/java/ru/betterend/interfaces/AnvilScreenHandlerExtended.java @@ -5,27 +5,29 @@ import java.util.List; import ru.betterend.recipe.builders.AnvilRecipe; public interface AnvilScreenHandlerExtended { - public void be_updateCurrentRecipe(AnvilRecipe recipe); - public AnvilRecipe be_getCurrentRecipe(); - public List be_getRecipes(); + void be_updateCurrentRecipe(AnvilRecipe recipe); + AnvilRecipe be_getCurrentRecipe(); + List be_getRecipes(); default void be_nextRecipe() { - List recipes = this.be_getRecipes(); - AnvilRecipe current = this.be_getCurrentRecipe(); + List recipes = be_getRecipes(); + if (recipes.size() < 2) return; + AnvilRecipe current = be_getCurrentRecipe(); int i = recipes.indexOf(current) + 1; if (i >= recipes.size()) { i = 0; } - this.be_updateCurrentRecipe(recipes.get(i)); + be_updateCurrentRecipe(recipes.get(i)); } default void be_previousRecipe() { - List recipes = this.be_getRecipes(); - AnvilRecipe current = this.be_getCurrentRecipe(); + List recipes = be_getRecipes(); + if (recipes.size() < 2) return; + AnvilRecipe current = be_getCurrentRecipe(); int i = recipes.indexOf(current) - 1; if (i <= 0) { i = recipes.size() - 1; } - this.be_updateCurrentRecipe(recipes.get(i)); + be_updateCurrentRecipe(recipes.get(i)); } } diff --git a/src/main/java/ru/betterend/mixin/common/AnvilBlockMixin.java b/src/main/java/ru/betterend/mixin/common/AnvilBlockMixin.java index 9e73f468..a422c461 100644 --- a/src/main/java/ru/betterend/mixin/common/AnvilBlockMixin.java +++ b/src/main/java/ru/betterend/mixin/common/AnvilBlockMixin.java @@ -1,5 +1,6 @@ package ru.betterend.mixin.common; +import net.minecraft.state.property.IntProperty; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -15,10 +16,16 @@ public class AnvilBlockMixin { @Inject(method = "getLandingState", at = @At("HEAD"), cancellable = true) private static void be_getLandingState(BlockState fallingState, CallbackInfoReturnable info) { if (fallingState.getBlock() instanceof EndAnvilBlock) { - int destruction = fallingState.get(BlockProperties.DESTRUCTION); - BlockState state = (destruction < 2) ? fallingState.with(BlockProperties.DESTRUCTION, destruction + 1) : null; - info.setReturnValue(state); - info.cancel(); + IntProperty destructionProperty = ((EndAnvilBlock) fallingState.getBlock()).getDestructionProperty(); + int destruction = fallingState.get(destructionProperty); + try { + BlockState state = fallingState.with(destructionProperty, destruction + 1); + info.setReturnValue(state); + info.cancel(); + } catch (Exception ex) { + info.setReturnValue(null); + info.cancel(); + } } } } diff --git a/src/main/java/ru/betterend/mixin/common/AnvilScreenHandlerMixin.java b/src/main/java/ru/betterend/mixin/common/AnvilScreenHandlerMixin.java index cc92009d..f672562f 100644 --- a/src/main/java/ru/betterend/mixin/common/AnvilScreenHandlerMixin.java +++ b/src/main/java/ru/betterend/mixin/common/AnvilScreenHandlerMixin.java @@ -4,6 +4,7 @@ import java.util.Collections; import java.util.List; import java.util.stream.Collectors; +import net.minecraft.screen.*; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -18,10 +19,6 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.item.ItemStack; import net.minecraft.recipe.RecipeManager; -import net.minecraft.screen.AnvilScreenHandler; -import net.minecraft.screen.ForgingScreenHandler; -import net.minecraft.screen.ScreenHandlerContext; -import net.minecraft.screen.ScreenHandlerType; import net.minecraft.tag.BlockTags; import ru.betterend.blocks.basis.EndAnvilBlock; import ru.betterend.interfaces.AnvilScreenHandlerExtended; @@ -32,11 +29,26 @@ public abstract class AnvilScreenHandlerMixin extends ForgingScreenHandler imple private List be_recipes = Collections.emptyList(); private AnvilRecipe be_currentRecipe; - + private Property anvilLevel; + public AnvilScreenHandlerMixin(ScreenHandlerType type, int syncId, PlayerInventory playerInventory, ScreenHandlerContext context) { super(type, syncId, playerInventory, context); } + + @Inject(method = "*", at = @At("TAIL")) + public void be_initAnvilLevel(int syncId, PlayerInventory inventory, ScreenHandlerContext context, CallbackInfo info) { + int anvLevel = context.run((world, blockPos) -> { + Block anvilBlock = world.getBlockState(blockPos).getBlock(); + if (anvilBlock instanceof EndAnvilBlock) { + return ((EndAnvilBlock) anvilBlock).getCraftingLevel(); + } + return 1; + }, 1); + Property anvilLevel = Property.create(); + anvilLevel.set(anvLevel); + this.anvilLevel = addProperty(anvilLevel); + } @Shadow public abstract void updateResult(); @@ -76,25 +88,19 @@ public abstract class AnvilScreenHandlerMixin extends ForgingScreenHandler imple @Inject(method = "updateResult", at = @At("HEAD"), cancellable = true) public void be_updateOutput(CallbackInfo info) { RecipeManager recipeManager = this.player.world.getRecipeManager(); - this.be_recipes = recipeManager.getAllMatches(AnvilRecipe.TYPE, input, player.world); + be_recipes = recipeManager.getAllMatches(AnvilRecipe.TYPE, input, player.world); if (be_recipes.size() > 0) { - this.context.run((world, blockPos) -> { - int anvilLevel; - Block anvilBlock = world.getBlockState(blockPos).getBlock(); - if (anvilBlock instanceof EndAnvilBlock) { - anvilLevel = ((EndAnvilBlock) anvilBlock).getCraftingLevel(); - } else { - anvilLevel = 1; - } - this.be_recipes = be_recipes.stream().filter(recipe -> + int anvilLevel = this.anvilLevel.get(); + be_recipes = be_recipes.stream().filter(recipe -> anvilLevel >= recipe.getAnvilLevel()).collect(Collectors.toList()); - }); if (be_recipes.size() > 0) { if (be_currentRecipe == null || !be_recipes.contains(be_currentRecipe)) { - this.be_currentRecipe = be_recipes.get(0); + be_currentRecipe = be_recipes.get(0); } - this.be_updateResult(); + be_updateResult(); info.cancel(); + } else { + be_currentRecipe = null; } } } diff --git a/src/main/java/ru/betterend/recipe/AnvilRecipes.java b/src/main/java/ru/betterend/recipe/AnvilRecipes.java index 9509c3c9..2a4db6a7 100644 --- a/src/main/java/ru/betterend/recipe/AnvilRecipes.java +++ b/src/main/java/ru/betterend/recipe/AnvilRecipes.java @@ -20,45 +20,46 @@ public class AnvilRecipes { .setDamage(3) .build(); + int anvilLevel = EndToolMaterial.AETERNIUM.getMiningLevel(); AnvilRecipe.Builder.create("aeternium_axe_head") .setInput(EndItems.AETERNIUM_INGOT) .setOutput(EndItems.AETERNIUM_AXE_HEAD) - .setAnvilLevel(EndToolMaterial.AETERNIUM.getDurability()) + .setAnvilLevel(anvilLevel) .setToolLevel(4) .setDamage(6) .build(); AnvilRecipe.Builder.create("aeternium_pickaxe_head") .setInput(EndItems.AETERNIUM_INGOT) .setOutput(EndItems.AETERNIUM_PICKAXE_HEAD) - .setAnvilLevel(EndToolMaterial.AETERNIUM.getDurability()) + .setAnvilLevel(anvilLevel) .setToolLevel(4) .setDamage(6) .build(); AnvilRecipe.Builder.create("aeternium_shovel_head") .setInput(EndItems.AETERNIUM_INGOT) .setOutput(EndItems.AETERNIUM_SHOVEL_HEAD) - .setAnvilLevel(EndToolMaterial.AETERNIUM.getDurability()) + .setAnvilLevel(anvilLevel) .setToolLevel(4) .setDamage(6) .build(); AnvilRecipe.Builder.create("aeternium_hoe_head") .setInput(EndItems.AETERNIUM_INGOT) .setOutput(EndItems.AETERNIUM_HOE_HEAD) - .setAnvilLevel(EndToolMaterial.AETERNIUM.getDurability()) + .setAnvilLevel(anvilLevel) .setToolLevel(4) .setDamage(6) .build(); AnvilRecipe.Builder.create("aeternium_hammer_head") .setInput(EndItems.AETERNIUM_INGOT) .setOutput(EndItems.AETERNIUM_HAMMER_HEAD) - .setAnvilLevel(EndToolMaterial.AETERNIUM.getDurability()) + .setAnvilLevel(anvilLevel) .setToolLevel(4) .setDamage(6) .build(); AnvilRecipe.Builder.create("aeternium_sword_blade") .setInput(EndItems.AETERNIUM_INGOT) .setOutput(EndItems.AETERNIUM_SWORD_BLADE) - .setAnvilLevel(EndToolMaterial.AETERNIUM.getDurability()) + .setAnvilLevel(anvilLevel) .setToolLevel(4) .setDamage(6) .build(); diff --git a/src/main/java/ru/betterend/recipe/builders/AnvilRecipe.java b/src/main/java/ru/betterend/recipe/builders/AnvilRecipe.java index 24aa4047..13cc06d4 100644 --- a/src/main/java/ru/betterend/recipe/builders/AnvilRecipe.java +++ b/src/main/java/ru/betterend/recipe/builders/AnvilRecipe.java @@ -161,6 +161,11 @@ public class AnvilRecipe implements Recipe, BetterEndRecipe { return Objects.hash(id, input, output, damage, toolLevel); } + @Override + public String toString() { + return "AnvilRecipe [" + id + "]"; + } + public static class Builder { private final static Builder INSTANCE = new Builder(); @@ -299,7 +304,5 @@ public class AnvilRecipe implements Recipe, BetterEndRecipe { packetBuffer.writeVarInt(recipe.anvilLevel); packetBuffer.writeVarInt(recipe.damage); } - - } } diff --git a/src/main/java/ru/betterend/registry/EndTags.java b/src/main/java/ru/betterend/registry/EndTags.java index 0a6c9cb2..218b97ea 100644 --- a/src/main/java/ru/betterend/registry/EndTags.java +++ b/src/main/java/ru/betterend/registry/EndTags.java @@ -22,6 +22,7 @@ import net.minecraft.world.biome.Biome.Category; import net.minecraft.world.gen.surfacebuilder.SurfaceConfig; import ru.betterend.BetterEnd; import ru.betterend.blocks.EndTerrainBlock; +import ru.betterend.blocks.basis.EndAnvilBlock; import ru.betterend.blocks.basis.PedestalBlock; import ru.betterend.blocks.basis.SimpleLeavesBlock; import ru.betterend.blocks.basis.VineBlock; @@ -113,6 +114,7 @@ public class EndTags { )); TagHelper.addTag(FURNACES, Blocks.FURNACE); + TagHelper.addTag(BlockTags.ANVIL, EndBlocks.AETERNIUM_ANVIL); } public static void addSurfaceBlock(Block block) {