[Change] Rebalanced Anvil Durability (#259)
This commit is contained in:
parent
fc2a68180b
commit
c9d990c65f
2 changed files with 36 additions and 27 deletions
|
@ -6,18 +6,22 @@ import org.betterx.bclib.client.models.PatternsHelper;
|
|||
import org.betterx.bclib.interfaces.BlockModelProvider;
|
||||
import org.betterx.bclib.interfaces.CustomItemProvider;
|
||||
import org.betterx.bclib.items.BaseAnvilItem;
|
||||
import org.betterx.bclib.util.BlocksHelper;
|
||||
|
||||
import net.minecraft.client.renderer.block.model.BlockModel;
|
||||
import net.minecraft.client.resources.model.UnbakedModel;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.util.RandomSource;
|
||||
import net.minecraft.world.item.BlockItem;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.PickaxeItem;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.AnvilBlock;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
import net.minecraft.world.level.block.LevelEvent;
|
||||
import net.minecraft.world.level.block.state.BlockBehaviour;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.block.state.StateDefinition;
|
||||
|
@ -36,6 +40,7 @@ import java.util.Collections;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public abstract class BaseAnvilBlock extends AnvilBlock implements BlockModelProvider, CustomItemProvider {
|
||||
|
@ -126,7 +131,7 @@ public abstract class BaseAnvilBlock extends AnvilBlock implements BlockModelPro
|
|||
public BlockState damageAnvilUse(BlockState state, RandomSource random) {
|
||||
IntegerProperty durability = getDurabilityProp();
|
||||
int value = state.getValue(durability);
|
||||
if (value < getMaxDurability() && random.nextInt(10) == 0) {
|
||||
if (value < getMaxDurability()) {
|
||||
return state.setValue(durability, value + 1);
|
||||
}
|
||||
value = state.getValue(DESTRUCTION);
|
||||
|
@ -137,4 +142,15 @@ public abstract class BaseAnvilBlock extends AnvilBlock implements BlockModelPro
|
|||
int destruction = state.getValue(DESTRUCTION);
|
||||
return destruction < 2 ? state.setValue(DESTRUCTION, destruction + 1) : null;
|
||||
}
|
||||
|
||||
@ApiStatus.Internal
|
||||
public static void destroyWhenNull(Level level, BlockPos blockPos, BlockState damaged) {
|
||||
if (damaged == null) {
|
||||
level.removeBlock(blockPos, false);
|
||||
level.levelEvent(LevelEvent.SOUND_ANVIL_BROKEN, blockPos, 0);
|
||||
} else {
|
||||
level.setBlock(blockPos, damaged, BlocksHelper.FLAG_SEND_CLIENT_CHANGES);
|
||||
level.levelEvent(LevelEvent.SOUND_ANVIL_USED, blockPos, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ import org.jetbrains.annotations.Nullable;
|
|||
|
||||
@Mixin(AnvilMenu.class)
|
||||
public abstract class AnvilMenuMixin extends ItemCombinerMenu implements AnvilScreenHandlerExtended {
|
||||
private List<AnvilRecipe> be_recipes = Collections.emptyList();
|
||||
private List<AnvilRecipe> bcl_recipes = Collections.emptyList();
|
||||
private AnvilRecipe bcl_currentRecipe;
|
||||
private DataSlot anvilLevel;
|
||||
|
||||
|
@ -79,22 +79,15 @@ public abstract class AnvilMenuMixin extends ItemCombinerMenu implements AnvilSc
|
|||
@Inject(method = "method_24922", at = @At(value = "HEAD"), cancellable = true)
|
||||
private static void bcl_onDamageAnvil(Player player, Level level, BlockPos blockPos, CallbackInfo ci) {
|
||||
BlockState blockState = level.getBlockState(blockPos);
|
||||
if (blockState.getBlock() instanceof BaseAnvilBlock anvil) {
|
||||
if (!player.getAbilities().instabuild
|
||||
&& blockState.getBlock() instanceof BaseAnvilBlock anvil
|
||||
&& player.getRandom().nextDouble() < 0.12) {
|
||||
BlockState damaged = anvil.damageAnvilUse(blockState, player.getRandom());
|
||||
bcl_destroyWhenNull(level, blockPos, damaged);
|
||||
BaseAnvilBlock.destroyWhenNull(level, blockPos, damaged);
|
||||
ci.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
private static void bcl_destroyWhenNull(Level level, BlockPos blockPos, BlockState damaged) {
|
||||
if (damaged == null) {
|
||||
level.removeBlock(blockPos, false);
|
||||
level.levelEvent(LevelEvent.SOUND_ANVIL_BROKEN, blockPos, 0);
|
||||
} else {
|
||||
level.setBlock(blockPos, damaged, 2);
|
||||
level.levelEvent(LevelEvent.SOUND_ANVIL_USED, blockPos, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(method = "onTake", at = @At("HEAD"), cancellable = true)
|
||||
protected void bcl_onTakeAnvilOutput(Player player, ItemStack stack, CallbackInfo info) {
|
||||
|
@ -107,12 +100,12 @@ public abstract class AnvilMenuMixin extends ItemCombinerMenu implements AnvilSc
|
|||
access.execute((level, blockPos) -> {
|
||||
final BlockState anvilState = level.getBlockState(blockPos);
|
||||
final Block anvilBlock = anvilState.getBlock();
|
||||
if (anvilBlock instanceof BaseAnvilBlock) {
|
||||
final BaseAnvilBlock anvil = (BaseAnvilBlock) anvilBlock;
|
||||
if (!player.getAbilities().instabuild && anvilState.is(BlockTags.ANVIL) && player.getRandom()
|
||||
.nextDouble() < 0.1) {
|
||||
if (anvilBlock instanceof BaseAnvilBlock anvil) {
|
||||
if (!player.getAbilities().instabuild
|
||||
&& anvilState.is(BlockTags.ANVIL)
|
||||
&& player.getRandom().nextDouble() < 0.1) {
|
||||
BlockState damagedState = anvil.damageAnvilUse(anvilState, player.getRandom());
|
||||
bcl_destroyWhenNull(level, blockPos, damagedState);
|
||||
BaseAnvilBlock.destroyWhenNull(level, blockPos, damagedState);
|
||||
} else {
|
||||
level.levelEvent(LevelEvent.SOUND_ANVIL_USED, blockPos, 0);
|
||||
}
|
||||
|
@ -125,15 +118,15 @@ public abstract class AnvilMenuMixin extends ItemCombinerMenu implements AnvilSc
|
|||
@Inject(method = "createResult", at = @At("HEAD"), cancellable = true)
|
||||
public void bcl_updateOutput(CallbackInfo info) {
|
||||
RecipeManager recipeManager = this.player.level().getRecipeManager();
|
||||
be_recipes = recipeManager.getRecipesFor(AnvilRecipe.TYPE, inputSlots, player.level());
|
||||
if (be_recipes.size() > 0) {
|
||||
bcl_recipes = recipeManager.getRecipesFor(AnvilRecipe.TYPE, inputSlots, player.level());
|
||||
if (bcl_recipes.size() > 0) {
|
||||
int anvilLevel = this.anvilLevel.get();
|
||||
be_recipes = be_recipes.stream()
|
||||
.filter(recipe -> anvilLevel >= recipe.getAnvilLevel())
|
||||
.collect(Collectors.toList());
|
||||
if (be_recipes.size() > 0) {
|
||||
if (bcl_currentRecipe == null || !be_recipes.contains(bcl_currentRecipe)) {
|
||||
bcl_currentRecipe = be_recipes.get(0);
|
||||
bcl_recipes = bcl_recipes.stream()
|
||||
.filter(recipe -> anvilLevel >= recipe.getAnvilLevel())
|
||||
.collect(Collectors.toList());
|
||||
if (bcl_recipes.size() > 0) {
|
||||
if (bcl_currentRecipe == null || !bcl_recipes.contains(bcl_currentRecipe)) {
|
||||
bcl_currentRecipe = bcl_recipes.get(0);
|
||||
}
|
||||
bcl_updateResult();
|
||||
info.cancel();
|
||||
|
@ -182,6 +175,6 @@ public abstract class AnvilMenuMixin extends ItemCombinerMenu implements AnvilSc
|
|||
|
||||
@Override
|
||||
public List<AnvilRecipe> bcl_getRecipes() {
|
||||
return be_recipes;
|
||||
return bcl_recipes;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue