Work around End Anvils

This commit is contained in:
Aleksey 2021-06-11 21:37:03 +03:00
parent 3668a4694a
commit 4f36d24ab2
11 changed files with 121 additions and 114 deletions

View file

@ -1,69 +1,33 @@
package ru.betterend.blocks;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import com.google.common.collect.Maps;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import ru.bclib.blocks.BlockProperties;
import ru.bclib.client.models.ModelsHelper;
import ru.betterend.blocks.basis.EndAnvilBlock;
import ru.betterend.client.models.Patterns;
import ru.betterend.item.EndAnvilItem;
import ru.betterend.item.material.EndToolMaterial;
import ru.betterend.registry.EndBlocks;
import ru.betterend.registry.EndItems;
public class AeterniumAnvil extends EndAnvilBlock {
private static final IntegerProperty DESTRUCTION_LONG = BlockProperties.DESTRUCTION_LONG;
protected final Item anvilItem;
public AeterniumAnvil() {
super(EndBlocks.AETERNIUM_BLOCK.defaultMaterialColor(), EndToolMaterial.AETERNIUM.getLevel());
}
@Override
public IntegerProperty getDestructionProperty() {
return DESTRUCTION_LONG;
this.anvilItem = EndItems.registerEndItem("aeternuim_anvil_item", new EndAnvilItem(this));
}
@Override
public IntegerProperty getDurability() {
if (durability == null) {
this.maxDamage = 8;
this.durability = IntegerProperty.create("durability", 0, maxDamage);
this.maxDurability = 8;
this.durability = IntegerProperty.create("durability", 0, maxDurability);
}
return durability;
}
@Override
public @Nullable BlockModel getBlockModel(ResourceLocation blockId, BlockState blockState) {
String name = blockId.getPath();
int damage = getDamageState(blockState);
Map<String, String> textures = Maps.newHashMap();
textures.put("%anvil%", name);
textures.put("%top%", name + "_top_" + damage);
Optional<String> pattern = Patterns.createJson(Patterns.BLOCK_ANVIL, textures);
return ModelsHelper.fromPattern(pattern);
}
@Override
public UnbakedModel getModelVariant(ResourceLocation stateId, BlockState blockState, Map<ResourceLocation, UnbakedModel> modelCache) {
int damage = getDamageState(blockState);
String modId = stateId.getNamespace();
String modelId = "block/" + stateId.getPath() + "_top_" + damage;
ResourceLocation modelLocation = new ResourceLocation(modId, modelId);
registerBlockModel(stateId, modelLocation, blockState, modelCache);
return ModelsHelper.createFacingModel(modelLocation, blockState.getValue(FACING), false, false);
}
private int getDamageState(BlockState blockState) {
IntegerProperty destructionProperty = getDestructionProperty();
int damage = blockState.getValue(destructionProperty);
return damage < 3 ? 0 : damage < 6 ? 1 : 2;
public Item asItem() {
return anvilItem;
}
}

View file

@ -4,15 +4,15 @@ import net.minecraft.util.StringRepresentable;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import ru.bclib.blocks.BlockProperties;
import ru.betterend.registry.EndPortals;
public class EndBlockProperties {
public class EndBlockProperties extends BlockProperties {
public static final EnumProperty<HydraluxShape> HYDRALUX_SHAPE = EnumProperty.create("shape", HydraluxShape.class);
public static final EnumProperty<PedestalState> PEDESTAL_STATE = EnumProperty.create("state", PedestalState.class);
public static final EnumProperty<CactusBottom> CACTUS_BOTTOM = EnumProperty.create("bottom", CactusBottom.class);
public static final BooleanProperty HAS_ITEM = BooleanProperty.create("has_item");
public static final IntegerProperty PORTAL = IntegerProperty.create("portal", 0, EndPortals.getCount());
public enum PedestalState implements StringRepresentable {

View file

@ -1,26 +1,39 @@
package ru.betterend.blocks.basis;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.block.AnvilBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.material.MaterialColor;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import net.minecraft.world.level.storage.loot.LootContext;
import ru.bclib.blocks.BaseAnvilBlock;
import ru.betterend.blocks.complex.MetalMaterial;
import ru.betterend.item.EndAnvilItem;
import java.util.List;
import java.util.Objects;
public class EndAnvilBlock extends BaseAnvilBlock {
protected final int level;
protected IntegerProperty durability;
protected int maxDamage;
protected MetalMaterial metalMaterial;
protected int maxDurability;
public EndAnvilBlock(MaterialColor color, int level) {
super(color);
this.level = level;
}
public EndAnvilBlock(MetalMaterial metalMaterial, MaterialColor color, int level) {
this(color, level);
this.metalMaterial = metalMaterial;
}
public int getDurability(BlockState blockState) {
Block anvilBlock = blockState.getBlock();
if (anvilBlock instanceof EndAnvilBlock) {
@ -31,14 +44,36 @@ public class EndAnvilBlock extends BaseAnvilBlock {
public IntegerProperty getDurability() {
if (durability == null) {
this.maxDamage = 5;
this.durability = IntegerProperty.create("durability", 0, maxDamage);
this.maxDurability = 5;
this.durability = IntegerProperty.create("durability", 0, maxDurability);
}
return durability;
}
public int getMaxDamage() {
return maxDamage;
public int getMaxDurability() {
return maxDurability;
}
@Override
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
List<ItemStack> drops = super.getDrops(state, builder);
ItemStack itemStack = drops.get(0);
itemStack.getOrCreateTag().putInt(EndAnvilItem.DURABILITY, state.getValue(durability));
return drops;
}
@Override
@SuppressWarnings("deprecation")
public Item asItem() {
if (metalMaterial != null) {
return metalMaterial.anvilItem;
}
return Item.byBlock(this);
}
@Override
public BlockState getStateForPlacement(BlockPlaceContext blockPlaceContext) {
return Objects.requireNonNull(super.getStateForPlacement(blockPlaceContext)).setValue(durability, maxDurability);
}
@Override
@ -56,11 +91,12 @@ public class EndAnvilBlock extends BaseAnvilBlock {
if (anvilBlock instanceof EndAnvilBlock) {
EndAnvilBlock endAnvilBlock = (EndAnvilBlock) anvilBlock;
IntegerProperty durability = endAnvilBlock.getDurability();
int damage = blockState.getValue(durability) + 1;
if (damage <= endAnvilBlock.getMaxDamage()) {
int damage = blockState.getValue(durability) - 1;
if (damage > 0) {
return blockState.setValue(durability, damage);
}
blockState = blockState.setValue(durability, 0);
int maxDurability = endAnvilBlock.getMaxDurability();
blockState = blockState.setValue(durability, maxDurability);
}
return getDamagedState(blockState);
}
@ -68,7 +104,7 @@ public class EndAnvilBlock extends BaseAnvilBlock {
private static BlockState getDamagedState(BlockState fallingState) {
Block anvilBlock = fallingState.getBlock();
if (anvilBlock instanceof EndAnvilBlock) {
IntegerProperty destructionProperty = ((EndAnvilBlock) anvilBlock).getDestructionProperty();
IntegerProperty destructionProperty = EndAnvilBlock.DESTRUCTION;
int destruction = fallingState.getValue(destructionProperty) + 1;
if (destructionProperty.getPossibleValues().contains(destruction)) {
try {

View file

@ -22,6 +22,7 @@ import ru.betterend.blocks.BulbVineLanternBlock;
import ru.betterend.blocks.BulbVineLanternColoredBlock;
import ru.betterend.blocks.ChandelierBlock;
import ru.betterend.blocks.basis.*;
import ru.betterend.item.EndAnvilItem;
import ru.betterend.item.EndArmorItem;
import ru.betterend.item.tool.EndHammerItem;
import ru.betterend.recipe.builders.*;
@ -37,7 +38,6 @@ public class MetalMaterial {
public final Block pressurePlate;
public final Block door;
public final Block trapdoor;
public final Block anvil;
public final Block chain;
public final Block stairs;
public final Block slab;
@ -45,7 +45,10 @@ public class MetalMaterial {
public final Block chandelier;
public final Block bulb_lantern;
public final ColoredMaterial bulb_lantern_colored;
public final Block anvilBlock;
public final Item anvilItem;
public final Item nugget;
public final Item ingot;
@ -96,7 +99,6 @@ public class MetalMaterial {
slab = EndBlocks.registerBlock(name + "_slab", new BaseSlabBlock(tile));
door = EndBlocks.registerBlock(name + "_door", new BaseDoorBlock(block));
trapdoor = EndBlocks.registerBlock(name + "_trapdoor", new BaseTrapdoorBlock(block));
anvil = EndBlocks.registerBlock(name + "_anvil", new EndAnvilBlock(block.defaultMaterialColor(), level));
bars = EndBlocks.registerBlock(name + "_bars", new BaseMetalBarsBlock(block));
chain = EndBlocks.registerBlock(name + "_chain", new BaseChainBlock(block.defaultMaterialColor()));
pressurePlate = EndBlocks.registerBlock(name + "_plate", new WoodenPressurePlateBlock(block));
@ -104,7 +106,7 @@ public class MetalMaterial {
chandelier = EndBlocks.registerBlock(name + "_chandelier", new ChandelierBlock(block));
bulb_lantern = EndBlocks.registerBlock(name + "_bulb_lantern", new BulbVineLanternBlock(lanternProperties));
bulb_lantern_colored = new ColoredMaterial(BulbVineLanternColoredBlock::new, bulb_lantern, false);
nugget = EndItems.registerEndItem(name + "_nugget", new ModelProviderItem(itemSettings));
ingot = EndItems.registerEndItem(name + "_ingot", new ModelProviderItem(itemSettings));
@ -127,6 +129,9 @@ public class MetalMaterial {
chestplate = EndItems.registerEndItem(name + "_chestplate", new EndArmorItem(armor, EquipmentSlot.CHEST, itemSettings));
leggings = EndItems.registerEndItem(name + "_leggings", new EndArmorItem(armor, EquipmentSlot.LEGS, itemSettings));
boots = EndItems.registerEndItem(name + "_boots", new EndArmorItem(armor, EquipmentSlot.FEET, itemSettings));
anvilBlock = EndBlocks.registerBlock(name + "_anvil", new EndAnvilBlock(this, block.defaultMaterialColor(), level));
anvilItem = EndItems.registerEndItem(name + "_anvil_item", new EndAnvilItem(anvilBlock));
if (hasOre) {
FurnaceRecipe.make(name + "_ingot_furnace", ore, ingot).setGroup("end_ingot").buildWithBlasting();
@ -148,7 +153,7 @@ public class MetalMaterial {
GridRecipe.make(name + "_stairs", stairs).setOutputCount(4).setShape("# ", "## ", "###").addMaterial('#', block, tile).setGroup("end_metal_stairs").build();
GridRecipe.make(name + "_slab", slab).setOutputCount(6).setShape("###").addMaterial('#', block, tile).setGroup("end_metal_slabs").build();
GridRecipe.make(name + "_chain", chain).setShape("N", "#", "N").addMaterial('#', ingot).addMaterial('N', nugget).setGroup("end_metal_chain").build();
GridRecipe.make(name + "_anvil", anvil).setShape("###", " I ", "III").addMaterial('#', block, tile).addMaterial('I', ingot).setGroup("end_metal_anvil").build();
GridRecipe.make(name + "_anvil", anvilBlock).setShape("###", " I ", "III").addMaterial('#', block, tile).addMaterial('I', ingot).setGroup("end_metal_anvil").build();
GridRecipe.make(name + "_bulb_lantern", bulb_lantern).setShape("C", "I", "#").addMaterial('C', chain).addMaterial('I', ingot).addMaterial('#', EndItems.GLOWING_BULB).build();
GridRecipe.make(name + "_chandelier", chandelier).setShape("I#I", " # ").addMaterial('#', ingot).addMaterial('I', EndItems.LUMECORN_ROD).setGroup("end_metal_chandelier").build();
@ -187,7 +192,7 @@ public class MetalMaterial {
GridRecipe.make(name + "_leggings", leggings).setShape("###", "# #", "# #").addMaterial('#', forgedPlate).setGroup("end_metal_leggings").build();
GridRecipe.make(name + "_boots", boots).setShape("# #", "# #").addMaterial('#', forgedPlate).setGroup("end_metal_boots").build();
TagHelper.addTag(BlockTags.ANVIL, anvil);
TagHelper.addTag(BlockTags.ANVIL, anvilBlock);
TagHelper.addTag(BlockTags.BEACON_BASE_BLOCKS, block);
TagHelper.addTag(ItemTags.BEACON_PAYMENT_ITEMS, ingot);
TagHelper.addTag(EndTags.DRAGON_IMMUNE, ore, bars);

View file

@ -2,6 +2,9 @@ package ru.betterend.item;
import java.util.List;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.api.EnvType;
@ -15,30 +18,34 @@ import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import ru.bclib.client.models.BlockModelProvider;
import ru.bclib.client.models.ItemModelProvider;
import ru.bclib.items.BaseAnvilItem;
import ru.betterend.blocks.basis.EndAnvilBlock;
import ru.betterend.registry.EndBlocks;
public class EndAnvilItem extends BlockItem {
public EndAnvilItem(Block block) {
super(block, EndBlocks.makeBlockItemSettings());
public class EndAnvilItem extends BaseAnvilItem {
public final static String DURABILITY = "durability";
public EndAnvilItem(Block anvilBlock) {
super(anvilBlock, EndBlocks.makeBlockItemSettings());
}
@Override
@SuppressWarnings("ConstantConditions")
protected BlockState getPlacementState(BlockPlaceContext blockPlaceContext) {
BlockState blockState = super.getPlacementState(blockPlaceContext);
ItemStack stack = blockPlaceContext.getItemInHand();
int level = stack.getOrCreateTag().getInt("level");
blockState = blockState.setValue(((EndAnvilBlock) blockState.getBlock()).getDestructionProperty(), level);
int durability = stack.getOrCreateTag().getInt(DURABILITY);
blockState = blockState.setValue(((EndAnvilBlock) blockState.getBlock()).getDurability(), durability);
return blockState;
}
@Override
@Environment(EnvType.CLIENT)
public void appendHoverText(ItemStack itemStack, @Nullable Level level, List<Component> list, TooltipFlag tooltipFlag) {
super.appendHoverText(itemStack, level, list, tooltipFlag);
int l = itemStack.getOrCreateTag().getInt("level");
if (l > 0) {
list.add(new TranslatableComponent("message.betterend.anvil_damage").append(": " + l));
}
public BlockModel getItemModel(ResourceLocation resourceLocation) {
Block block = getBlock();
ResourceLocation blockId = Registry.BLOCK.getKey(block);
return ((ItemModelProvider) block).getItemModel(blockId);
}
}

View file

@ -1,33 +1,27 @@
package ru.betterend.mixin.common;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.*;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
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.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AnvilMenu;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.inventory.DataSlot;
import net.minecraft.world.inventory.ItemCombinerMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.world.level.block.AnvilBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import ru.betterend.blocks.basis.EndAnvilBlock;
import ru.betterend.interfaces.AnvilScreenHandlerExtended;
import ru.betterend.recipe.builders.AnvilRecipe;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@Mixin(AnvilMenu.class)
public abstract class AnvilMenuMixin extends ItemCombinerMenu implements AnvilScreenHandlerExtended {
private List<AnvilRecipe> be_recipes = Collections.emptyList();
@ -73,7 +67,7 @@ public abstract class AnvilMenuMixin extends ItemCombinerMenu implements AnvilSc
slotsChanged(inputSlots);
access.execute((world, blockPos) -> {
BlockState anvilState = world.getBlockState(blockPos);
if (!player.abilities.instabuild && anvilState.is(BlockTags.ANVIL) && player.getRandom().nextFloat() < 0.12F) {
if (!player.abilities.instabuild && anvilState.is(BlockTags.ANVIL) && player.getRandom().nextDouble() < 0.1) {
BlockState landingState = EndAnvilBlock.applyDamage(anvilState);
if (landingState == null) {
world.removeBlock(blockPos, false);

View file

@ -73,13 +73,13 @@ public class SmithingRecipes {
.build();
SmithingTableRecipe.create("thallasium_anvil_updrade")
.setResult(EndBlocks.TERMINITE.anvil)
.setBase(EndBlocks.THALLASIUM.anvil)
.setResult(EndBlocks.TERMINITE.anvilBlock)
.setBase(EndBlocks.THALLASIUM.anvilBlock)
.setAddition(EndBlocks.TERMINITE.block)
.build();
SmithingTableRecipe.create("terminite_anvil_updrade")
.setResult(EndBlocks.AETERNIUM_ANVIL)
.setBase(EndBlocks.TERMINITE.anvil)
.setBase(EndBlocks.TERMINITE.anvilBlock)
.setAddition(EndItems.AETERNIUM_INGOT)
.build();

View file

@ -40,17 +40,17 @@ public class EndBlocks extends BlocksRegistry {
public static final Block RUTISCUS = registerBlock("rutiscus", new EndTerrainBlock(MaterialColor.COLOR_ORANGE));
// Roads //
public static final Block END_MYCELIUM_PATH = registerBlock("end_mycelium_path", new EndPathBlock(END_MYCELIUM));
public static final Block END_MOSS_PATH = registerBlock("end_moss_path", new EndPathBlock(END_MOSS));
public static final Block CHORUS_NYLIUM_PATH = registerBlock("chorus_nylium_path", new EndPathBlock(CHORUS_NYLIUM));
public static final Block CAVE_MOSS_PATH = registerBlock("cave_moss_path", new EndPathBlock(CAVE_MOSS));
public static final Block CRYSTAL_MOSS_PATH = registerBlock("crystal_moss_path", new EndPathBlock(CRYSTAL_MOSS));
public static final Block SHADOW_GRASS_PATH = registerBlock("shadow_grass_path", new EndPathBlock(SHADOW_GRASS));
public static final Block PINK_MOSS_PATH = registerBlock("pink_moss_path", new EndPathBlock(PINK_MOSS));
public static final Block AMBER_MOSS_PATH = registerBlock("amber_moss_path", new EndPathBlock(AMBER_MOSS));
public static final Block JUNGLE_MOSS_PATH = registerBlock("jungle_moss_path", new EndPathBlock(JUNGLE_MOSS));
public static final Block SANGNUM_PATH = registerBlock("sangnum_path", new EndPathBlock(SANGNUM));
public static final Block RUTISCUS_PATH = registerBlock("rutiscus_path", new EndPathBlock(RUTISCUS));
public static final Block END_MYCELIUM_PATH = registerBlock("end_mycelium_path", new BasePathBlock(END_MYCELIUM));
public static final Block END_MOSS_PATH = registerBlock("end_moss_path", new BasePathBlock(END_MOSS));
public static final Block CHORUS_NYLIUM_PATH = registerBlock("chorus_nylium_path", new BasePathBlock(CHORUS_NYLIUM));
public static final Block CAVE_MOSS_PATH = registerBlock("cave_moss_path", new BasePathBlock(CAVE_MOSS));
public static final Block CRYSTAL_MOSS_PATH = registerBlock("crystal_moss_path", new BasePathBlock(CRYSTAL_MOSS));
public static final Block SHADOW_GRASS_PATH = registerBlock("shadow_grass_path", new BasePathBlock(SHADOW_GRASS));
public static final Block PINK_MOSS_PATH = registerBlock("pink_moss_path", new BasePathBlock(PINK_MOSS));
public static final Block AMBER_MOSS_PATH = registerBlock("amber_moss_path", new BasePathBlock(AMBER_MOSS));
public static final Block JUNGLE_MOSS_PATH = registerBlock("jungle_moss_path", new BasePathBlock(JUNGLE_MOSS));
public static final Block SANGNUM_PATH = registerBlock("sangnum_path", new BasePathBlock(SANGNUM));
public static final Block RUTISCUS_PATH = registerBlock("rutiscus_path", new BasePathBlock(RUTISCUS));
public static final Block MOSSY_OBSIDIAN = registerBlock("mossy_obsidian", new MossyObsidian());
public static final Block DRAGON_BONE_BLOCK = registerBlock("dragon_bone_block", new BaseRotatedPillarBlock(Blocks.BONE_BLOCK));

View file

@ -116,6 +116,7 @@ public class EndItems extends ItemsRegistry {
public final static Item CHORUS_MUSHROOM_COOKED = registerEndFood("chorus_mushroom_cooked", Foods.MUSHROOM_STEW);
public final static Item BOLUX_MUSHROOM_COOKED = registerEndFood("bolux_mushroom_cooked", Foods.MUSHROOM_STEW);
public final static Item CAVE_PUMPKIN_PIE = registerEndFood("cave_pumpkin_pie", Foods.PUMPKIN_PIE);
// Drinks //
public final static Item UMBRELLA_CLUSTER_JUICE = registerEndDrink("umbrella_cluster_juice", 5, 0.7F);

View file

@ -46,7 +46,7 @@
"fabricloader": ">=0.11.0",
"fabric": ">=0.32.0",
"minecraft": ">=1.16.4",
"bclib": ">=0.1.28"
"bclib": ">=0.1.30"
},
"suggests": {
"byg": ">=1.1.3",