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);