Skip to content

Commit

Permalink
Adopt material stripping in tools
Browse files Browse the repository at this point in the history
More work required to make sure custom tools actually do their thing
  • Loading branch information
Aizistral committed Sep 7, 2023
1 parent 88baa51 commit 2271708
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@

import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraftforge.common.ForgeHooks;

public interface IBaseTool {
public Set<Material> getEffectiveMaterials();
public float getEfficiency();

default boolean canHarvestBlock(BlockState blockIn, Player player) {
if (ForgeHooks.isCorrectToolForDrops(blockIn, player))
return true;

Material material = blockIn.getMaterial();
return this.getEffectiveMaterials().contains(material);
return ForgeHooks.isCorrectToolForDrops(blockIn, player);
}

/*default float getDestroySpeed(ItemStack stack, BlockState state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Random;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Predicate;

import javax.annotation.Nullable;

Expand All @@ -25,7 +26,6 @@
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
Expand All @@ -34,7 +34,7 @@ public class AOEMiningHelper {
public static final Random random = new Random();

/** Attempt to break blocks around the given pos in a 3x3x1 square relative to the targeted face.*/
public static void attemptBreakNeighbors(Level world, BlockPos pos, Player player, Set<Block> effectiveOn, Set<Material> effectiveMaterials, boolean checkHarvestLevel) {
public static void attemptBreakNeighbors(Level world, BlockPos pos, Player player, Set<Block> effectiveOn, Predicate<BlockState> predicate, boolean checkHarvestLevel) {
HitResult trace = AOEMiningHelper.calcRayTrace(world, player, ClipContext.Fluid.ANY);

if (trace.getType() == HitResult.Type.BLOCK) {
Expand Down Expand Up @@ -62,7 +62,7 @@ public static void attemptBreakNeighbors(Level world, BlockPos pos, Player playe
target = pos.offset(0, a, b);
}

AOEMiningHelper.attemptBreak(world, target, player, effectiveOn, effectiveMaterials, fortuneLevel, silkLevel, checkHarvestLevel, null, (objPos, objState) -> {
AOEMiningHelper.attemptBreak(world, target, player, effectiveOn, predicate, fortuneLevel, silkLevel, checkHarvestLevel, null, (objPos, objState) -> {
});
}
}
Expand All @@ -78,12 +78,12 @@ public static void attemptBreakNeighbors(Level world, BlockPos pos, Player playe
* or not damage the tool used for mining the block.
*/

public static void attemptBreak(Level world, BlockPos pos, Player player, Set<Block> effectiveOn, Set<Material> effectiveMaterials, int fortuneLevel, int silkLevel, boolean checkHarvestLevel, ItemStack tool, BiConsumer<BlockPos, BlockState> toolDamageConsumer) {
public static void attemptBreak(Level world, BlockPos pos, Player player, Set<Block> effectiveOn, Predicate<BlockState> predicate, int fortuneLevel, int silkLevel, boolean checkHarvestLevel, ItemStack tool, BiConsumer<BlockPos, BlockState> toolDamageConsumer) {
BlockState state = world.getBlockState(pos);
BlockEntity iCertainlyHopeYouHaveATileEntityLicense = world.getBlockEntity(pos);

boolean validHarvest = !checkHarvestLevel || player.getMainHandItem().isCorrectToolForDrops(state);
boolean isEffective = effectiveOn.contains(state.getBlock()) || effectiveMaterials.contains(state.getMaterial());
boolean isEffective = effectiveOn.contains(state.getBlock()) || predicate.test(state);
boolean unbreakable = state.is(BlockTags.WITHER_IMMUNE) || state.getBlock() == Blocks.SPAWNER || state.getDestroySpeed(world, pos) < 0F;

if (validHarvest && isEffective && !unbreakable) {
Expand Down Expand Up @@ -125,7 +125,7 @@ public static Vector3 calcRayTrace(Level worldIn, Player player, ClipContext.Flu
}
}

public static void harvestPlane(Level world, Player player, Direction dir, BlockPos pos, Set<Material> effectiveMaterials, int radius, boolean harvestLevelCheck, @Nullable BlockPos excludedBlock, ItemStack tool, BiConsumer<BlockPos, BlockState> toolDamageConsumer) {
public static void harvestPlane(Level world, Player player, Direction dir, BlockPos pos, Predicate<BlockState> predicate, int radius, boolean harvestLevelCheck, @Nullable BlockPos excludedBlock, ItemStack tool, BiConsumer<BlockPos, BlockState> toolDamageConsumer) {
int fortuneLevel = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.BLOCK_FORTUNE, player.getMainHandItem());
int silkLevel = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.SILK_TOUCH, player.getMainHandItem());
int supRad = (radius - 1) / 2;
Expand All @@ -149,12 +149,12 @@ public static void harvestPlane(Level world, Player player, Direction dir, Block
continue;
}

AOEMiningHelper.attemptBreak(world, target, player, Sets.newHashSet(), effectiveMaterials, fortuneLevel, silkLevel, harvestLevelCheck, tool, toolDamageConsumer);
AOEMiningHelper.attemptBreak(world, target, player, Sets.newHashSet(), predicate, fortuneLevel, silkLevel, harvestLevelCheck, tool, toolDamageConsumer);
}
}
}

public static void harvestCube(Level world, Player player, Direction dir, BlockPos centralPos, Set<Material> effectiveMaterials, int planeRadius, int depth, boolean harvestLevelCheck, @Nullable BlockPos excludedBlock, ItemStack tool, BiConsumer<BlockPos, BlockState> toolDamageConsumer) {
public static void harvestCube(Level world, Player player, Direction dir, BlockPos centralPos, Predicate<BlockState> predicate, int planeRadius, int depth, boolean harvestLevelCheck, @Nullable BlockPos excludedBlock, ItemStack tool, BiConsumer<BlockPos, BlockState> toolDamageConsumer) {

for (int a = 0; a < depth; a++) {
int x = 0;
Expand All @@ -180,7 +180,7 @@ public static void harvestCube(Level world, Player player, Direction dir, BlockP
x += a;
}

AOEMiningHelper.harvestPlane(world, player, dir, new BlockPos(centralPos).offset(x, y, z), effectiveMaterials, planeRadius, harvestLevelCheck, excludedBlock, tool, toolDamageConsumer);
AOEMiningHelper.harvestPlane(world, player, dir, new BlockPos(centralPos).offset(x, y, z), predicate, planeRadius, harvestLevelCheck, excludedBlock, tool, toolDamageConsumer);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import net.minecraftforge.common.ToolActions;
import net.minecraftforge.network.PacketDistributor;

// TODO Make sure Astral Breaker actually works
public class AstralBreaker extends ItemBaseTool implements IMultiblockMiningTool {
public static Omniconfig.IntParameter miningRadius;
public static Omniconfig.IntParameter miningDepth;
Expand Down Expand Up @@ -90,10 +91,6 @@ public AstralBreaker() {
.defaultDurability(4000)
.rarity(Rarity.EPIC)
.fireResistant());

this.effectiveMaterials.addAll(((EtheriumPickaxe) this.findTool("etherium_pickaxe")).effectiveMaterials);
this.effectiveMaterials.addAll(((EtheriumAxe) this.findTool("etherium_axe")).effectiveMaterials);
this.effectiveMaterials.addAll(((EtheriumShovel) this.findTool("etherium_shovel")).effectiveMaterials);
}

private Item findTool(String name) {
Expand Down Expand Up @@ -130,14 +127,14 @@ public boolean mineBlock(ItemStack stack, Level world, BlockState state, BlockPo
this.spawnFlameParticles(world, pos);
}

if (entityLiving instanceof Player player && this.areaEffectsEnabled(player, stack) && this.effectiveMaterials.contains(state.getMaterial()) && !world.isClientSide && miningRadius.getValue() != -1) {
if (entityLiving instanceof Player player && this.areaEffectsEnabled(player, stack) /*&& this.effectiveMaterials.contains(state.getMaterial())*/ && !world.isClientSide && miningRadius.getValue() != -1) {
HitResult trace = AOEMiningHelper.calcRayTrace(world, player, ClipContext.Fluid.ANY);

if (trace.getType() == HitResult.Type.BLOCK) {
BlockHitResult blockTrace = (BlockHitResult) trace;
Direction face = blockTrace.getDirection();

AOEMiningHelper.harvestCube(world, player, face, pos, this.effectiveMaterials, miningRadius.getValue() + EtheriumConfigHandler.instance().getAOEBoost(player), miningDepth.getValue(), true, pos, stack, (objPos, objState) -> {
AOEMiningHelper.harvestCube(world, player, face, pos, (s) -> false, miningRadius.getValue() + EtheriumConfigHandler.instance().getAOEBoost(player), miningDepth.getValue(), true, pos, stack, (objPos, objState) -> {
stack.hurtAndBreak(1, entityLiving, p -> p.broadcastBreakEvent(Mob.getEquipmentSlotForItem(stack)));
this.spawnFlameParticles(world, objPos);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.BucketPickup;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.block.SpongeBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.AABB;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
Expand Down Expand Up @@ -161,7 +161,6 @@ public void curioTick(SlotContext context, ItemStack stack) {
player.getCooldowns().addCooldown(this, 20);

}

}
}
}
Expand All @@ -171,7 +170,7 @@ public void absorbWaterBlock(BlockPos pos, BlockState state, Level world) {
// Whatever
} else if (state.getBlock() instanceof LiquidBlock) {
world.setBlock(pos, Blocks.AIR.defaultBlockState(), 3);
} else if (state.getMaterial() == Material.WATER_PLANT || state.getMaterial() == Material.REPLACEABLE_WATER_PLANT) {
} else if (!state.is(Blocks.KELP) && !state.is(Blocks.KELP_PLANT) && !state.is(Blocks.SEAGRASS) && !state.is(Blocks.TALL_SEAGRASS)) {
BlockEntity tileentity = state.hasBlockEntity() ? world.getBlockEntity(pos) : null;
Block.dropResources(state, world, pos, tileentity);
world.setBlock(pos, Blocks.AIR.defaultBlockState(), 3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@
import net.minecraft.world.item.Tier;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraftforge.common.ToolAction;

public abstract class ItemBaseTool extends DiggerItem implements ICreativeTabMember {
public Set<Material> effectiveMaterials;
public Set<ToolAction> toolActions;

public ItemBaseTool(float attackDamageIn, float attackSpeedIn, Tier tier, TagKey<Block> effectiveBlocksIn, Properties builder) {
super(attackDamageIn, attackSpeedIn, tier, effectiveBlocksIn, builder);

this.effectiveMaterials = Sets.newHashSet();
this.toolActions = Sets.newHashSet();
}

Expand All @@ -40,14 +36,13 @@ public ItemBaseTool() {
}

@Override
public boolean isCorrectToolForDrops(ItemStack stack, BlockState blockIn) {
return super.isCorrectToolForDrops(stack, blockIn) || this.effectiveMaterials.contains(blockIn.getMaterial());
public boolean isCorrectToolForDrops(ItemStack stack, BlockState blockIn) { // TODO Something about this
return super.isCorrectToolForDrops(stack, blockIn) /*|| this.effectiveMaterials.contains(blockIn.getMaterial())*/;
}

@Override
public float getDestroySpeed(ItemStack stack, BlockState state) {
Material material = state.getMaterial();
return !this.effectiveMaterials.contains(material) ? super.getDestroySpeed(stack, state) : this.speed;
return !this.isCorrectToolForDrops(stack, state) ? super.getDestroySpeed(stack, state) : this.speed;
}

@Override
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/com/aizistral/etherium/core/IEtheriumConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Tier;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.material.Material;
import net.minecraftforge.fml.ModList;

public interface IEtheriumConfig {
Expand Down Expand Up @@ -60,18 +59,4 @@ public interface IEtheriumConfig {

public boolean isStandalone();

public default Optional<Material> getSorceryMaterial(String name) {
if (ModList.get().isLoaded("astralsorcery")) {
try {
Class<?> sorceryBlockMaterials = Class.forName("hellfirepvp.astralsorcery.common.lib.MaterialsAS");
Material material = (Material) sorceryBlockMaterials.getField(name).get(null);
return Optional.ofNullable(material);
} catch (Exception ex) {
ex.printStackTrace();
}
}

return Optional.empty();
}

}
20 changes: 3 additions & 17 deletions src/main/java/com/aizistral/etherium/items/EtheriumAxe.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,13 @@
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

public class EtheriumAxe extends AxeItem implements IEtheriumTool, ICreativeTabMember {
public Set<Material> effectiveMaterials;

public EtheriumAxe() {
super(EnigmaticMaterials.ETHERIUM, 10, -3.2F, EtheriumUtil.defaultProperties(EtheriumAxe.class).fireResistant());

this.effectiveMaterials = Sets.newHashSet();
this.effectiveMaterials.add(Material.WOOD);
this.effectiveMaterials.add(Material.LEAVES);
this.effectiveMaterials.add(Material.CACTUS);
this.effectiveMaterials.add(Material.BAMBOO);
this.effectiveMaterials.add(Material.NETHER_WOOD);
this.effectiveMaterials.add(Material.BAMBOO_SAPLING);
this.effectiveMaterials.add(Material.VEGETABLE);

this.getConfig().getSorceryMaterial("INFUSED_WOOD").ifPresent(this.effectiveMaterials::add);
}

@Override
Expand Down Expand Up @@ -88,11 +75,11 @@ public void appendHoverText(ItemStack stack, @Nullable Level worldIn, List<Compo

@Override
public boolean mineBlock(ItemStack stack, Level world, BlockState state, BlockPos pos, LivingEntity entityLiving) {
if (entityLiving instanceof Player && this.areaEffectsEnabled((Player) entityLiving, stack) && this.effectiveMaterials.contains(state.getMaterial()) && !world.isClientSide && this.getConfig().getAxeMiningVolume() != -1) {
if (entityLiving instanceof Player && this.areaEffectsEnabled((Player) entityLiving, stack) && /*this.effectiveMaterials.contains(state.getMaterial()) &&*/ !world.isClientSide && this.getConfig().getAxeMiningVolume() != -1) {
Direction face = Direction.UP;
int volume = this.getConfig().getAxeMiningVolume() + (this.getConfig().getAOEBoost((Player) entityLiving));

AOEMiningHelper.harvestCube(world, (Player) entityLiving, face, pos.offset(0, (volume - 1) / 2, 0), this.effectiveMaterials, volume, volume, false, pos, stack, (objPos, objState) -> {
AOEMiningHelper.harvestCube(world, (Player) entityLiving, face, pos.offset(0, (volume - 1) / 2, 0), (s) -> false, volume, volume, false, pos, stack, (objPos, objState) -> {
stack.hurtAndBreak(1, entityLiving, p -> p.broadcastBreakEvent(Mob.getEquipmentSlotForItem(stack)));
});
}
Expand All @@ -102,8 +89,7 @@ public boolean mineBlock(ItemStack stack, Level world, BlockState state, BlockPo

@Override
public float getDestroySpeed(ItemStack stack, BlockState state) {
Material material = state.getMaterial();
return !this.effectiveMaterials.contains(material) ? super.getDestroySpeed(stack, state) : this.speed;
return !this.isCorrectToolForDrops(stack, state) ? super.getDestroySpeed(stack, state) : this.speed;
}

@Override
Expand Down
17 changes: 2 additions & 15 deletions src/main/java/com/aizistral/etherium/items/EtheriumPickaxe.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraftforge.api.distmarker.Dist;
Expand All @@ -46,18 +45,6 @@ public EtheriumPickaxe() {
EtheriumUtil.defaultProperties(EtheriumPickaxe.class)
.defaultDurability((int) (EtheriumConfigHandler.instance().getToolMaterial().getUses() * 1.5))
.fireResistant());

this.effectiveMaterials.add(Material.METAL);
this.effectiveMaterials.add(Material.STONE);
this.effectiveMaterials.add(Material.HEAVY_METAL);
this.effectiveMaterials.add(Material.GLASS);
this.effectiveMaterials.add(Material.ICE_SOLID);
this.effectiveMaterials.add(Material.ICE);
this.effectiveMaterials.add(Material.SHULKER_SHELL);
this.effectiveMaterials.add(Material.AMETHYST);

this.getConfig().getSorceryMaterial("MARBLE").ifPresent(this.effectiveMaterials::add);
this.getConfig().getSorceryMaterial("BLACK_MARBLE").ifPresent(this.effectiveMaterials::add);
}

@Override
Expand Down Expand Up @@ -86,13 +73,13 @@ public void appendHoverText(ItemStack stack, @Nullable Level worldIn, List<Compo

@Override
public boolean mineBlock(ItemStack stack, Level world, BlockState state, BlockPos pos, LivingEntity entityLiving) {
if (entityLiving instanceof Player && this.areaEffectsEnabled((Player) entityLiving, stack) && this.effectiveMaterials.contains(state.getMaterial()) && !world.isClientSide && this.getConfig().getPickaxeMiningRadius() != -1) {
if (entityLiving instanceof Player && this.areaEffectsEnabled((Player) entityLiving, stack) && this.isCorrectToolForDrops(stack, state) && !world.isClientSide && this.getConfig().getPickaxeMiningRadius() != -1) {
HitResult trace = AOEMiningHelper.calcRayTrace(world, (Player) entityLiving, ClipContext.Fluid.ANY);
if (trace.getType() == HitResult.Type.BLOCK) {
BlockHitResult blockTrace = (BlockHitResult) trace;
Direction face = blockTrace.getDirection();

AOEMiningHelper.harvestCube(world, (Player) entityLiving, face, pos, this.effectiveMaterials, this.getConfig().getPickaxeMiningRadius() + this.getConfig().getAOEBoost((Player) entityLiving), this.getConfig().getPickaxeMiningDepth(), true, pos, stack, (objPos, objState) -> {
AOEMiningHelper.harvestCube(world, (Player) entityLiving, face, pos, (s) -> this.isCorrectToolForDrops(stack, s), this.getConfig().getPickaxeMiningRadius() + this.getConfig().getAOEBoost((Player) entityLiving), this.getConfig().getPickaxeMiningDepth(), true, pos, stack, (objPos, objState) -> {
stack.hurtAndBreak(1, entityLiving, p -> p.broadcastBreakEvent(Mob.getEquipmentSlotForItem(stack)));
});
}
Expand Down
Loading

0 comments on commit 2271708

Please sign in to comment.