Anvil fixes
This commit is contained in:
parent
cba8637122
commit
1a4a9ef0a1
6 changed files with 168 additions and 22 deletions
|
@ -8,6 +8,7 @@ import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
|
|||
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
|
||||
import net.minecraft.client.renderer.block.model.BlockModel;
|
||||
import net.minecraft.client.resources.model.UnbakedModel;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.world.item.BlockItem;
|
||||
import net.minecraft.world.item.Item;
|
||||
|
@ -27,13 +28,16 @@ import ru.bclib.client.models.PatternsHelper;
|
|||
import ru.bclib.interfaces.BlockModelProvider;
|
||||
import ru.bclib.interfaces.CustomItemProvider;
|
||||
import ru.bclib.items.BaseAnvilItem;
|
||||
import ru.bclib.util.MHelper;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
|
||||
public abstract class BaseAnvilBlock extends AnvilBlock implements BlockModelProvider, CustomItemProvider {
|
||||
public static final IntegerProperty DESTRUCTION = BlockProperties.DESTRUCTION;
|
||||
public IntegerProperty durability;
|
||||
|
||||
public BaseAnvilBlock(MaterialColor color) {
|
||||
super(FabricBlockSettings.copyOf(Blocks.ANVIL).mapColor(color));
|
||||
|
@ -42,24 +46,13 @@ public abstract class BaseAnvilBlock extends AnvilBlock implements BlockModelPro
|
|||
@Override
|
||||
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
|
||||
super.createBlockStateDefinition(builder);
|
||||
builder.add(DESTRUCTION);
|
||||
if (getMaxDurability() != 3) {
|
||||
durability = IntegerProperty.create("durability", 0, getMaxDurability());
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("deprecation")
|
||||
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
|
||||
ItemStack dropStack = new ItemStack(this);
|
||||
int destruction = state.getValue(DESTRUCTION);
|
||||
dropStack.getOrCreateTag().putInt(BaseAnvilItem.DESTRUCTION, destruction);
|
||||
return Lists.newArrayList(dropStack);
|
||||
else {
|
||||
durability = BlockProperties.DEFAULT_ANVIL_DURABILITY;
|
||||
}
|
||||
|
||||
protected String getTop(ResourceLocation blockId, String block) {
|
||||
if (block.contains("item")) {
|
||||
return blockId.getPath() + "_top_0";
|
||||
}
|
||||
char last = block.charAt(block.length() - 1);
|
||||
return blockId.getPath() + "_top_" + last;
|
||||
builder.add(DESTRUCTION, durability);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -96,4 +89,39 @@ public abstract class BaseAnvilBlock extends AnvilBlock implements BlockModelPro
|
|||
public BlockItem getCustomItem(ResourceLocation blockID, FabricItemSettings settings) {
|
||||
return new BaseAnvilItem(this, settings);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("deprecation")
|
||||
public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
|
||||
int destruction = state.getValue(DESTRUCTION);
|
||||
int durability = state.getValue(getDurabilityProp());
|
||||
int value = destruction * getMaxDurability() + durability;
|
||||
List<ItemStack> drops = super.getDrops(state, builder);
|
||||
ItemStack itemStack = drops.get(0);
|
||||
itemStack.getOrCreateTag().putInt(BaseAnvilItem.DESTRUCTION, value);
|
||||
return drops;
|
||||
}
|
||||
|
||||
public IntegerProperty getDurabilityProp() {
|
||||
return durability;
|
||||
}
|
||||
|
||||
public int getMaxDurability() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
public BlockState damageAnvilUse(BlockState state, Random random) {
|
||||
IntegerProperty durability = getDurabilityProp();
|
||||
int value = state.getValue(durability);
|
||||
if (value < getMaxDurability() && random.nextInt(8) == 0) {
|
||||
return state.setValue(durability, value + 1);
|
||||
}
|
||||
value = state.getValue(DESTRUCTION);
|
||||
return value < 2 ? state.setValue(DESTRUCTION, value + 1).setValue(durability, 0) : null;
|
||||
}
|
||||
|
||||
public BlockState damageAnvilFall(BlockState state) {
|
||||
int destruction = state.getValue(DESTRUCTION);
|
||||
return destruction < 2 ? state.setValue(DESTRUCTION, destruction + 1) : null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ public class BlockProperties {
|
|||
public static final BooleanProperty ACTIVE = BooleanProperty.create("active");
|
||||
public static final BooleanProperty SMALL = BooleanProperty.create("small");
|
||||
|
||||
public static final IntegerProperty DEFAULT_ANVIL_DURABILITY = IntegerProperty.create("durability", 0, 3);
|
||||
public static final IntegerProperty DESTRUCTION = IntegerProperty.create("destruction", 0, 2);
|
||||
public static final IntegerProperty ROTATION = IntegerProperty.create("rotation", 0, 3);
|
||||
public static final IntegerProperty FULLNESS = IntegerProperty.create("fullness", 0, 3);
|
||||
|
|
|
@ -14,12 +14,14 @@ 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 net.minecraft.world.level.block.state.properties.IntegerProperty;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import ru.bclib.blocks.BaseAnvilBlock;
|
||||
import ru.bclib.interfaces.BlockModelProvider;
|
||||
import ru.bclib.interfaces.ItemModelProvider;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class BaseAnvilItem extends BlockItem implements ItemModelProvider {
|
||||
public final static String DESTRUCTION = "destruction";
|
||||
|
@ -34,7 +36,16 @@ public class BaseAnvilItem extends BlockItem implements ItemModelProvider {
|
|||
ItemStack stack = blockPlaceContext.getItemInHand();
|
||||
int destruction = stack.getOrCreateTag().getInt(DESTRUCTION);
|
||||
if (blockState != null) {
|
||||
blockState = blockState.setValue(BaseAnvilBlock.DESTRUCTION, destruction);
|
||||
BaseAnvilBlock block = (BaseAnvilBlock) blockState.getBlock();
|
||||
IntegerProperty durabilityProp = block.getDurabilityProp();
|
||||
if (destruction == 0) {
|
||||
blockState = blockState.setValue(durabilityProp, 0).setValue(BaseAnvilBlock.DESTRUCTION, 0);
|
||||
}
|
||||
else {
|
||||
int destructionValue = destruction / block.getMaxDurability();
|
||||
int durabilityValue = destruction - destructionValue * block.getMaxDurability();
|
||||
blockState = blockState.setValue(durabilityProp, durabilityValue).setValue(BaseAnvilBlock.DESTRUCTION, destructionValue);
|
||||
}
|
||||
}
|
||||
return blockState;
|
||||
}
|
||||
|
@ -42,10 +53,13 @@ public class BaseAnvilItem extends BlockItem implements ItemModelProvider {
|
|||
@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(DESTRUCTION);
|
||||
if (l > 0) {
|
||||
list.add(new TranslatableComponent("message.bclib.anvil_damage").append(": " + l));
|
||||
int destruction = itemStack.getOrCreateTag().getInt(DESTRUCTION);
|
||||
if (destruction > 0) {
|
||||
BaseAnvilBlock block = (BaseAnvilBlock) ((BaseAnvilItem) itemStack.getItem()).getBlock();
|
||||
int maxValue = block.getMaxDurability() * 3;
|
||||
float damage = maxValue - destruction;
|
||||
String percents = String.format(Locale.ROOT, "%.0F%%", damage);
|
||||
list.add(new TranslatableComponent("message.bclib.anvil_damage").append(": " + percents));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
20
src/main/java/ru/bclib/mixin/common/AnvilBlockMixin.java
Normal file
20
src/main/java/ru/bclib/mixin/common/AnvilBlockMixin.java
Normal file
|
@ -0,0 +1,20 @@
|
|||
package ru.bclib.mixin.common;
|
||||
|
||||
import net.minecraft.world.level.block.AnvilBlock;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
import ru.bclib.blocks.BaseAnvilBlock;
|
||||
|
||||
@Mixin(AnvilBlock.class)
|
||||
public class AnvilBlockMixin {
|
||||
@Inject(method = "damage", at = @At("HEAD"), cancellable = true)
|
||||
private static void bclib_anvilDamage(BlockState state, CallbackInfoReturnable<BlockState> info) {
|
||||
if (state.getBlock() instanceof BaseAnvilBlock) {
|
||||
BaseAnvilBlock anvil = (BaseAnvilBlock) state.getBlock();
|
||||
info.setReturnValue(anvil.damageAnvilFall(state));
|
||||
}
|
||||
}
|
||||
}
|
81
src/main/java/ru/bclib/mixin/common/AnvilMenuMixin.java
Normal file
81
src/main/java/ru/bclib/mixin/common/AnvilMenuMixin.java
Normal file
|
@ -0,0 +1,81 @@
|
|||
package ru.bclib.mixin.common;
|
||||
|
||||
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.level.block.AnvilBlock;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.spongepowered.asm.mixin.Final;
|
||||
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 ru.bclib.blocks.BaseAnvilBlock;
|
||||
|
||||
@Mixin(AnvilMenu.class)
|
||||
public abstract class AnvilMenuMixin extends ItemCombinerMenu {
|
||||
@Shadow
|
||||
private int repairItemCountCost;
|
||||
|
||||
@Final
|
||||
@Shadow
|
||||
private DataSlot cost;
|
||||
|
||||
public AnvilMenuMixin(@Nullable MenuType<?> menuType, int i, Inventory inventory, ContainerLevelAccess containerLevelAccess) {
|
||||
super(menuType, i, inventory, containerLevelAccess);
|
||||
}
|
||||
|
||||
@Inject(method = "onTake", at = @At("HEAD"), cancellable = true)
|
||||
protected void bclib_onTakeAnvilOutput(Player player, ItemStack stack, CallbackInfo info) {
|
||||
this.access.execute((level, blockPos) -> {
|
||||
BlockState blockState = level.getBlockState(blockPos);
|
||||
if (blockState.getBlock() instanceof BaseAnvilBlock) {
|
||||
if (!player.getAbilities().instabuild) {
|
||||
player.giveExperienceLevels(-this.cost.get());
|
||||
}
|
||||
|
||||
this.inputSlots.setItem(0, ItemStack.EMPTY);
|
||||
if (this.repairItemCountCost > 0) {
|
||||
ItemStack itemStack2 = this.inputSlots.getItem(1);
|
||||
if (!itemStack2.isEmpty() && itemStack2.getCount() > this.repairItemCountCost) {
|
||||
itemStack2.shrink(this.repairItemCountCost);
|
||||
this.inputSlots.setItem(1, itemStack2);
|
||||
}
|
||||
else {
|
||||
this.inputSlots.setItem(1, ItemStack.EMPTY);
|
||||
}
|
||||
}
|
||||
else {
|
||||
this.inputSlots.setItem(1, ItemStack.EMPTY);
|
||||
}
|
||||
|
||||
this.cost.set(0);
|
||||
|
||||
if (!player.getAbilities().instabuild && blockState.is(BlockTags.ANVIL) && player.getRandom()
|
||||
.nextFloat() < 0.12F) {
|
||||
BlockState blockState2 = AnvilBlock.damage(blockState);
|
||||
if (blockState2 == null) {
|
||||
level.removeBlock(blockPos, false);
|
||||
level.levelEvent(1029, blockPos, 0);
|
||||
}
|
||||
else {
|
||||
level.setBlock(blockPos, blockState2, 2);
|
||||
level.levelEvent(1030, blockPos, 0);
|
||||
}
|
||||
}
|
||||
else {
|
||||
level.levelEvent(1030, blockPos, 0);
|
||||
}
|
||||
}
|
||||
info.cancel();
|
||||
});
|
||||
}
|
||||
}
|
|
@ -12,6 +12,8 @@
|
|||
"RecipeManagerMixin",
|
||||
"BoneMealItemMixin",
|
||||
"ServerLevelMixin",
|
||||
"AnvilBlockMixin",
|
||||
"AnvilMenuMixin",
|
||||
"TagLoaderMixin"
|
||||
],
|
||||
"injectors": {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue