Anvil crafting fixes

This commit is contained in:
Aleksey 2021-02-13 00:15:11 +03:00
parent c5182a4418
commit 7e367a1971
9 changed files with 76 additions and 83 deletions

View file

@ -8,68 +8,37 @@ import java.util.Map;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.AnvilBlock; import net.minecraft.block.*;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.loot.context.LootContext; import net.minecraft.loot.context.LootContext;
import net.minecraft.state.StateManager; import net.minecraft.state.StateManager;
import net.minecraft.state.property.IntProperty; import net.minecraft.state.property.IntProperty;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
import ru.betterend.blocks.basis.EndAnvilBlock;
import ru.betterend.item.material.EndToolMaterial; import ru.betterend.item.material.EndToolMaterial;
import ru.betterend.patterns.BlockPatterned; import ru.betterend.patterns.BlockPatterned;
import ru.betterend.patterns.Patterns; import ru.betterend.patterns.Patterns;
import ru.betterend.registry.EndBlocks; 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; public static final IntProperty DESTRUCTION = BlockProperties.DESTRUCTION_LONG;
private final int level;
public AeterniumAnvil() { public AeterniumAnvil() {
super(FabricBlockSettings.copyOf(Blocks.ANVIL).materialColor(EndBlocks.AETERNIUM_BLOCK.getDefaultMaterialColor())); super(EndBlocks.AETERNIUM_BLOCK.getDefaultMaterialColor(), EndToolMaterial.AETERNIUM.getMiningLevel());
this.level = EndToolMaterial.AETERNIUM.getMiningLevel();
} }
@Override @Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) { protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
super.appendProperties(builder);
builder.add(DESTRUCTION); builder.add(DESTRUCTION);
builder.add(FACING);
} }
public int getCraftingLevel() {
return level;
}
@Override @Override
public List<ItemStack> getDroppedStacks(BlockState state, LootContext.Builder builder) { public IntProperty getDestructionProperty() {
return Collections.singletonList(new ItemStack(this)); 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<String, String> 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 @Override
public Identifier statePatternId() { public Identifier statePatternId() {
return Patterns.STATE_ANVIL_LONG; return Patterns.STATE_ANVIL_LONG;

View file

@ -25,7 +25,7 @@ import ru.betterend.patterns.Patterns;
public class EndAnvilBlock extends AnvilBlock implements BlockPatterned { public class EndAnvilBlock extends AnvilBlock implements BlockPatterned {
public static final IntProperty DESTRUCTION = BlockProperties.DESTRUCTION; public static final IntProperty DESTRUCTION = BlockProperties.DESTRUCTION;
private final int level; protected final int level;
public EndAnvilBlock(MaterialColor color, int level) { public EndAnvilBlock(MaterialColor color, int level) {
super(FabricBlockSettings.copyOf(Blocks.ANVIL).materialColor(color)); super(FabricBlockSettings.copyOf(Blocks.ANVIL).materialColor(color));
@ -38,6 +38,10 @@ public class EndAnvilBlock extends AnvilBlock implements BlockPatterned {
builder.add(DESTRUCTION); builder.add(DESTRUCTION);
} }
public IntProperty getDestructionProperty() {
return DESTRUCTION;
}
public int getCraftingLevel() { public int getCraftingLevel() {
return level; return level;
} }
@ -61,8 +65,8 @@ public class EndAnvilBlock extends AnvilBlock implements BlockPatterned {
map.put("%top%", getTop(blockId, block)); map.put("%top%", getTop(blockId, block));
return Patterns.createJson(Patterns.BLOCK_ANVIL, map); 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")) { if (block.contains("item")) {
return blockId.getPath() + "_top_0"; return blockId.getPath() + "_top_0";
} }

View file

@ -17,8 +17,7 @@ import ru.betterend.util.JsonFactory;
public final class ConfigKeeper { public final class ConfigKeeper {
private Map<ConfigKey, Entry<?>> configEntries = Maps.newHashMap(); private final Map<ConfigKey, Entry<?>> configEntries = Maps.newHashMap();
private final JsonObject configObject; private final JsonObject configObject;
private final ConfigWriter writer; private final ConfigWriter writer;

View file

@ -5,27 +5,29 @@ import java.util.List;
import ru.betterend.recipe.builders.AnvilRecipe; import ru.betterend.recipe.builders.AnvilRecipe;
public interface AnvilScreenHandlerExtended { public interface AnvilScreenHandlerExtended {
public void be_updateCurrentRecipe(AnvilRecipe recipe); void be_updateCurrentRecipe(AnvilRecipe recipe);
public AnvilRecipe be_getCurrentRecipe(); AnvilRecipe be_getCurrentRecipe();
public List<AnvilRecipe> be_getRecipes(); List<AnvilRecipe> be_getRecipes();
default void be_nextRecipe() { default void be_nextRecipe() {
List<AnvilRecipe> recipes = this.be_getRecipes(); List<AnvilRecipe> recipes = be_getRecipes();
AnvilRecipe current = this.be_getCurrentRecipe(); if (recipes.size() < 2) return;
AnvilRecipe current = be_getCurrentRecipe();
int i = recipes.indexOf(current) + 1; int i = recipes.indexOf(current) + 1;
if (i >= recipes.size()) { if (i >= recipes.size()) {
i = 0; i = 0;
} }
this.be_updateCurrentRecipe(recipes.get(i)); be_updateCurrentRecipe(recipes.get(i));
} }
default void be_previousRecipe() { default void be_previousRecipe() {
List<AnvilRecipe> recipes = this.be_getRecipes(); List<AnvilRecipe> recipes = be_getRecipes();
AnvilRecipe current = this.be_getCurrentRecipe(); if (recipes.size() < 2) return;
AnvilRecipe current = be_getCurrentRecipe();
int i = recipes.indexOf(current) - 1; int i = recipes.indexOf(current) - 1;
if (i <= 0) { if (i <= 0) {
i = recipes.size() - 1; i = recipes.size() - 1;
} }
this.be_updateCurrentRecipe(recipes.get(i)); be_updateCurrentRecipe(recipes.get(i));
} }
} }

View file

@ -1,5 +1,6 @@
package ru.betterend.mixin.common; package ru.betterend.mixin.common;
import net.minecraft.state.property.IntProperty;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
@ -15,10 +16,16 @@ public class AnvilBlockMixin {
@Inject(method = "getLandingState", at = @At("HEAD"), cancellable = true) @Inject(method = "getLandingState", at = @At("HEAD"), cancellable = true)
private static void be_getLandingState(BlockState fallingState, CallbackInfoReturnable<BlockState> info) { private static void be_getLandingState(BlockState fallingState, CallbackInfoReturnable<BlockState> info) {
if (fallingState.getBlock() instanceof EndAnvilBlock) { if (fallingState.getBlock() instanceof EndAnvilBlock) {
int destruction = fallingState.get(BlockProperties.DESTRUCTION); IntProperty destructionProperty = ((EndAnvilBlock) fallingState.getBlock()).getDestructionProperty();
BlockState state = (destruction < 2) ? fallingState.with(BlockProperties.DESTRUCTION, destruction + 1) : null; int destruction = fallingState.get(destructionProperty);
info.setReturnValue(state); try {
info.cancel(); BlockState state = fallingState.with(destructionProperty, destruction + 1);
info.setReturnValue(state);
info.cancel();
} catch (Exception ex) {
info.setReturnValue(null);
info.cancel();
}
} }
} }
} }

View file

@ -4,6 +4,7 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import net.minecraft.screen.*;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At; 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.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.recipe.RecipeManager; 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 net.minecraft.tag.BlockTags;
import ru.betterend.blocks.basis.EndAnvilBlock; import ru.betterend.blocks.basis.EndAnvilBlock;
import ru.betterend.interfaces.AnvilScreenHandlerExtended; import ru.betterend.interfaces.AnvilScreenHandlerExtended;
@ -32,11 +29,26 @@ public abstract class AnvilScreenHandlerMixin extends ForgingScreenHandler imple
private List<AnvilRecipe> be_recipes = Collections.emptyList(); private List<AnvilRecipe> be_recipes = Collections.emptyList();
private AnvilRecipe be_currentRecipe; private AnvilRecipe be_currentRecipe;
private Property anvilLevel;
public AnvilScreenHandlerMixin(ScreenHandlerType<?> type, int syncId, PlayerInventory playerInventory, public AnvilScreenHandlerMixin(ScreenHandlerType<?> type, int syncId, PlayerInventory playerInventory,
ScreenHandlerContext context) { ScreenHandlerContext context) {
super(type, syncId, playerInventory, context); super(type, syncId, playerInventory, context);
} }
@Inject(method = "<init>*", 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 @Shadow
public abstract void updateResult(); public abstract void updateResult();
@ -76,25 +88,19 @@ public abstract class AnvilScreenHandlerMixin extends ForgingScreenHandler imple
@Inject(method = "updateResult", at = @At("HEAD"), cancellable = true) @Inject(method = "updateResult", at = @At("HEAD"), cancellable = true)
public void be_updateOutput(CallbackInfo info) { public void be_updateOutput(CallbackInfo info) {
RecipeManager recipeManager = this.player.world.getRecipeManager(); 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) { if (be_recipes.size() > 0) {
this.context.run((world, blockPos) -> { int anvilLevel = this.anvilLevel.get();
int anvilLevel; be_recipes = be_recipes.stream().filter(recipe ->
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 ->
anvilLevel >= recipe.getAnvilLevel()).collect(Collectors.toList()); anvilLevel >= recipe.getAnvilLevel()).collect(Collectors.toList());
});
if (be_recipes.size() > 0) { if (be_recipes.size() > 0) {
if (be_currentRecipe == null || !be_recipes.contains(be_currentRecipe)) { 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(); info.cancel();
} else {
be_currentRecipe = null;
} }
} }
} }

View file

@ -20,45 +20,46 @@ public class AnvilRecipes {
.setDamage(3) .setDamage(3)
.build(); .build();
int anvilLevel = EndToolMaterial.AETERNIUM.getMiningLevel();
AnvilRecipe.Builder.create("aeternium_axe_head") AnvilRecipe.Builder.create("aeternium_axe_head")
.setInput(EndItems.AETERNIUM_INGOT) .setInput(EndItems.AETERNIUM_INGOT)
.setOutput(EndItems.AETERNIUM_AXE_HEAD) .setOutput(EndItems.AETERNIUM_AXE_HEAD)
.setAnvilLevel(EndToolMaterial.AETERNIUM.getDurability()) .setAnvilLevel(anvilLevel)
.setToolLevel(4) .setToolLevel(4)
.setDamage(6) .setDamage(6)
.build(); .build();
AnvilRecipe.Builder.create("aeternium_pickaxe_head") AnvilRecipe.Builder.create("aeternium_pickaxe_head")
.setInput(EndItems.AETERNIUM_INGOT) .setInput(EndItems.AETERNIUM_INGOT)
.setOutput(EndItems.AETERNIUM_PICKAXE_HEAD) .setOutput(EndItems.AETERNIUM_PICKAXE_HEAD)
.setAnvilLevel(EndToolMaterial.AETERNIUM.getDurability()) .setAnvilLevel(anvilLevel)
.setToolLevel(4) .setToolLevel(4)
.setDamage(6) .setDamage(6)
.build(); .build();
AnvilRecipe.Builder.create("aeternium_shovel_head") AnvilRecipe.Builder.create("aeternium_shovel_head")
.setInput(EndItems.AETERNIUM_INGOT) .setInput(EndItems.AETERNIUM_INGOT)
.setOutput(EndItems.AETERNIUM_SHOVEL_HEAD) .setOutput(EndItems.AETERNIUM_SHOVEL_HEAD)
.setAnvilLevel(EndToolMaterial.AETERNIUM.getDurability()) .setAnvilLevel(anvilLevel)
.setToolLevel(4) .setToolLevel(4)
.setDamage(6) .setDamage(6)
.build(); .build();
AnvilRecipe.Builder.create("aeternium_hoe_head") AnvilRecipe.Builder.create("aeternium_hoe_head")
.setInput(EndItems.AETERNIUM_INGOT) .setInput(EndItems.AETERNIUM_INGOT)
.setOutput(EndItems.AETERNIUM_HOE_HEAD) .setOutput(EndItems.AETERNIUM_HOE_HEAD)
.setAnvilLevel(EndToolMaterial.AETERNIUM.getDurability()) .setAnvilLevel(anvilLevel)
.setToolLevel(4) .setToolLevel(4)
.setDamage(6) .setDamage(6)
.build(); .build();
AnvilRecipe.Builder.create("aeternium_hammer_head") AnvilRecipe.Builder.create("aeternium_hammer_head")
.setInput(EndItems.AETERNIUM_INGOT) .setInput(EndItems.AETERNIUM_INGOT)
.setOutput(EndItems.AETERNIUM_HAMMER_HEAD) .setOutput(EndItems.AETERNIUM_HAMMER_HEAD)
.setAnvilLevel(EndToolMaterial.AETERNIUM.getDurability()) .setAnvilLevel(anvilLevel)
.setToolLevel(4) .setToolLevel(4)
.setDamage(6) .setDamage(6)
.build(); .build();
AnvilRecipe.Builder.create("aeternium_sword_blade") AnvilRecipe.Builder.create("aeternium_sword_blade")
.setInput(EndItems.AETERNIUM_INGOT) .setInput(EndItems.AETERNIUM_INGOT)
.setOutput(EndItems.AETERNIUM_SWORD_BLADE) .setOutput(EndItems.AETERNIUM_SWORD_BLADE)
.setAnvilLevel(EndToolMaterial.AETERNIUM.getDurability()) .setAnvilLevel(anvilLevel)
.setToolLevel(4) .setToolLevel(4)
.setDamage(6) .setDamage(6)
.build(); .build();

View file

@ -161,6 +161,11 @@ public class AnvilRecipe implements Recipe<Inventory>, BetterEndRecipe {
return Objects.hash(id, input, output, damage, toolLevel); return Objects.hash(id, input, output, damage, toolLevel);
} }
@Override
public String toString() {
return "AnvilRecipe [" + id + "]";
}
public static class Builder { public static class Builder {
private final static Builder INSTANCE = new Builder(); private final static Builder INSTANCE = new Builder();
@ -299,7 +304,5 @@ public class AnvilRecipe implements Recipe<Inventory>, BetterEndRecipe {
packetBuffer.writeVarInt(recipe.anvilLevel); packetBuffer.writeVarInt(recipe.anvilLevel);
packetBuffer.writeVarInt(recipe.damage); packetBuffer.writeVarInt(recipe.damage);
} }
} }
} }

View file

@ -22,6 +22,7 @@ import net.minecraft.world.biome.Biome.Category;
import net.minecraft.world.gen.surfacebuilder.SurfaceConfig; import net.minecraft.world.gen.surfacebuilder.SurfaceConfig;
import ru.betterend.BetterEnd; import ru.betterend.BetterEnd;
import ru.betterend.blocks.EndTerrainBlock; import ru.betterend.blocks.EndTerrainBlock;
import ru.betterend.blocks.basis.EndAnvilBlock;
import ru.betterend.blocks.basis.PedestalBlock; import ru.betterend.blocks.basis.PedestalBlock;
import ru.betterend.blocks.basis.SimpleLeavesBlock; import ru.betterend.blocks.basis.SimpleLeavesBlock;
import ru.betterend.blocks.basis.VineBlock; import ru.betterend.blocks.basis.VineBlock;
@ -113,6 +114,7 @@ public class EndTags {
)); ));
TagHelper.addTag(FURNACES, Blocks.FURNACE); TagHelper.addTag(FURNACES, Blocks.FURNACE);
TagHelper.addTag(BlockTags.ANVIL, EndBlocks.AETERNIUM_ANVIL);
} }
public static void addSurfaceBlock(Block block) { public static void addSurfaceBlock(Block block) {