diff --git a/src/main/java/redart15/commandly/CommandlyMod.java b/src/main/java/redart15/commandly/CommandlyMod.java index 8f62a7d..5306071 100644 --- a/src/main/java/redart15/commandly/CommandlyMod.java +++ b/src/main/java/redart15/commandly/CommandlyMod.java @@ -1,45 +1,65 @@ package redart15.commandly; import net.fabricmc.api.ModInitializer; +import net.fabricmc.loader.api.FabricLoader; +import net.fabricmc.loader.api.entrypoint.EntrypointContainer; import net.minecraft.core.data.gamerule.GameRuleBoolean; import net.minecraft.core.data.gamerule.GameRules; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import redart15.commandly.api.CommandlyPlugin; import redart15.commandly.veincapitator.OreGroups; +import redart15.commandly.veincapitator.PickAxeRegister; import turniplabs.halplibe.util.GameStartEntrypoint; import turniplabs.halplibe.util.RecipeEntrypoint; public class CommandlyMod implements ModInitializer, RecipeEntrypoint, GameStartEntrypoint { public static final String MOD_ID = "commandly"; public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); - public static final int MASK = 7; - public static final int MAX_BLOCK_COUNT = 256 * 16 * 16 * 25; - public static GameRuleBoolean MOSS_SPREADING = GameRules.register(new GameRuleBoolean("doMossSpreading", true)); - public static GameRuleBoolean GRASS_SPREADING = GameRules.register(new GameRuleBoolean("doGrassSpreading", true)); - public static GameRuleBoolean VEIN_MINING = GameRules.register(new GameRuleBoolean("veinmining", false)); + private static final int MASK = 7; + private static final int MAX_BLOCK_COUNT = 256 * 16 * 16 * 25; + public static final GameRuleBoolean MOSS_SPREADING = GameRules.register(new GameRuleBoolean("doMossSpreading", true)); + public static final GameRuleBoolean GRASS_SPREADING = GameRules.register(new GameRuleBoolean("doGrassSpreading", true)); + public static final GameRuleBoolean VEINMINING = GameRules.register(new GameRuleBoolean("veinmining", false)); + @Override public void onInitialize() { - OreGroups.init(); LOGGER.info("Commandly initialized"); + // no need } @Override public void beforeGameStart() { - + // no need } @Override public void afterGameStart() { + LOGGER.info("Loading implementation"); + OreGroups.init(); + PickAxeRegister.init(); + FabricLoader.getInstance() + .getEntrypointContainers("commandly", CommandlyPlugin.class) + .forEach(CommandlyMod::initialize); } + private static void initialize(EntrypointContainer plugin) { + CommandlyPlugin entrypoint = plugin.getEntrypoint(); + entrypoint.registerOreGroups(OreGroups.getInstance()); + entrypoint.registerPickaxe(PickAxeRegister.getInstance()); + } + + public static int getMask(){return MASK;} + public static int getMaxBlockCount(){return MAX_BLOCK_COUNT;} + @Override public void onRecipesReady() { - + // no need } @Override public void initNamespaces() { - + // no need } } diff --git a/src/main/java/redart15/commandly/api/CommandlyPlugin.java b/src/main/java/redart15/commandly/api/CommandlyPlugin.java new file mode 100644 index 0000000..68bc6d8 --- /dev/null +++ b/src/main/java/redart15/commandly/api/CommandlyPlugin.java @@ -0,0 +1,9 @@ +package redart15.commandly.api; + +import redart15.commandly.veincapitator.OreGroups; +import redart15.commandly.veincapitator.PickAxeRegister; + +public interface CommandlyPlugin { + void registerOreGroups(OreGroups registry); + void registerPickaxe(PickAxeRegister register); +} diff --git a/src/main/java/redart15/commandly/command/CommandProtect.java b/src/main/java/redart15/commandly/command/CommandProtect.java index a65ad06..df9e6ee 100644 --- a/src/main/java/redart15/commandly/command/CommandProtect.java +++ b/src/main/java/redart15/commandly/command/CommandProtect.java @@ -23,7 +23,6 @@ import java.util.function.Predicate; import static redart15.commandly.command.CommandlyCommands.ReturnValues.*; -import static redart15.commandly.CommandlyMod.MASK; @SuppressWarnings("ALL") //cause this drives me nuts public class CommandProtect implements CommandManager.CommandRegistry { @@ -98,7 +97,7 @@ private static int chunks_protect(CommandSource source, World world, Predicate block = world.getBlock(fx + x, y, fz + z); if (block == null) continue; if (treecapitator.test(block) || veinmining.test(block)) { - world.setBlockMetadata(fx + x, y, fz + z, (1 << MASK)); + world.setBlockMetadata(fx + x, y, fz + z, (1 << CommandlyMod.getMask())); count_protected++; } } @@ -157,7 +156,7 @@ private static int radius_protect( throw new RuntimeException(e); } - if ((int) Math.floor(1.25 * Math.PI * Math.pow(radius, 3)) > CommandlyMod.MAX_BLOCK_COUNT) { + if ((int) Math.floor(1.25 * Math.PI * Math.pow(radius, 3)) > CommandlyMod.getMaxBlockCount()) { source.sendTranslatableMessage("commadly.protected.tolarge"); return code(CANNOT); } @@ -169,7 +168,7 @@ private static int radius_protect( Block block = world.getBlock(fx + x, fy + y, fz + z); if (block == null) continue; if (treecapitator.test(block) || veinmining.test(block)) { - world.setBlockMetadata(fx + x, fy + y, fz + z, (1 << MASK)); + world.setBlockMetadata(fx + x, fy + y, fz + z, (1 << CommandlyMod.getMask())); count_protected++; } } @@ -229,7 +228,7 @@ public static int point_protect( } catch (CommandSyntaxException e) { throw new RuntimeException(e); } - if (Math.abs(fx - sx) * Math.abs(fy - sy) * Math.abs(fz - sz) > CommandlyMod.MAX_BLOCK_COUNT) { + if (Math.abs(fx - sx) * Math.abs(fy - sy) * Math.abs(fz - sz) > CommandlyMod.getMaxBlockCount()) { source.sendTranslatableMessage("commadly.protected.toolarge"); return code(CANNOT); } @@ -241,7 +240,7 @@ public static int point_protect( Block block = world.getBlock(x, y, z); if (block == null) continue; if (treecapitator.test(block) || veinmining.test(block)) { - world.setBlockMetadata(x, y, z, (1 << MASK)); + world.setBlockMetadata(x, y, z, (1 << CommandlyMod.getMask())); count_protected++; } } diff --git a/src/main/java/redart15/commandly/mixins/mixin/ItemToolPickaxeMixinVeinMining.java b/src/main/java/redart15/commandly/mixins/mixin/ItemToolPickaxeMixinVeinMining.java index 17306a7..a9f848c 100644 --- a/src/main/java/redart15/commandly/mixins/mixin/ItemToolPickaxeMixinVeinMining.java +++ b/src/main/java/redart15/commandly/mixins/mixin/ItemToolPickaxeMixinVeinMining.java @@ -22,7 +22,7 @@ protected ItemToolPickaxeMixinVeinMining(String name, String namespaceId, int id @Override public boolean beforeDestroyBlock(World world, ItemStack itemStack, int blockId, int x, int y, int z, Side side, Player player) { - if (!world.isClientSide && world.getGameRuleValue(CommandlyMod.VEIN_MINING) && !player.isSneaking()) { + if (!world.isClientSide && world.getGameRuleValue(CommandlyMod.VEINMINING) && !player.isSneaking()) { return !VeinMining.veinMining(world, itemStack, x, y, z, player).mine(blockId, side); } return true; diff --git a/src/main/java/redart15/commandly/mixins/mixin/StopGrassSpreadingMixin.java b/src/main/java/redart15/commandly/mixins/mixin/StopGrassSpreadingMixin.java index 311799b..47aec44 100644 --- a/src/main/java/redart15/commandly/mixins/mixin/StopGrassSpreadingMixin.java +++ b/src/main/java/redart15/commandly/mixins/mixin/StopGrassSpreadingMixin.java @@ -1,101 +1,27 @@ package redart15.commandly.mixins.mixin; -import net.minecraft.core.block.Block; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; import net.minecraft.core.block.BlockLogicGrass; -import net.minecraft.core.block.Blocks; -import net.minecraft.core.data.gamerule.GameRules; import net.minecraft.core.world.World; -import net.minecraft.core.world.biome.Biome; -import net.minecraft.core.world.biome.Biomes; 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.CallbackInfo; import redart15.commandly.CommandlyMod; -import java.util.Random; @Mixin(value = BlockLogicGrass.class, remap = false) public abstract class StopGrassSpreadingMixin{ - - @Inject(method = "updateTick", at = @At("HEAD"), cancellable = true) - public void updateTick(World world, int x, int y, int z, Random rand, CallbackInfo ci) { - - if (world.isClientSide) { - return; - } - - BlockLogicGrass self = (BlockLogicGrass)(Object)this; - - Block dirt = self.dirt; - Block block = self.block; - - if (revertToDirt(world, x, y, z, rand, dirt)) return; - if (world.getBlockLightValue(x, y + 1, z) < 9) { - return; - } - spreadGrass(world, x, y, z, rand, dirt, block); - spreadsFlowers(world, x, y, z, rand); - ci.cancel(); - } - - private void spreadGrass(World world, int x, int y, int z, Random rand, Block dirt, Block block) { - if(!world.getGameRuleValue(CommandlyMod.GRASS_SPREADING)) { - return; - } - for(int i = 0; i < 4; ++i) { - int x1 = x + rand.nextInt(3) - 1; - int y1 = y + rand.nextInt(5) - 3; - int z1 = z + rand.nextInt(3) - 1; - if (world.getBlockId(x1, y1, z1) == dirt.id() && world.getBlockLightValue(x1, y1 + 1, z1) >= 4 && Blocks.lightBlock[world.getBlockId(x1, y1 + 1, z1)] <= 2) { - world.setBlockWithNotify(x1, y1, z1, block.id()); - } - } - } - - private boolean revertToDirt(World world, int x, int y, int z, Random rand, Block dirt) { - if (world.getBlockLightValue(x, y + 1, z) < 4 && Blocks.lightBlock[world.getBlockId(x, y + 1, z)] > 2) { - if (rand.nextInt(4) == 0) { - world.setBlockWithNotify(x, y, z, dirt.id()); - } - return true; - } - return false; - } - - private void spreadsFlowers(World world, int x, int y, int z, Random rand) { - if (!((Boolean) world.getGameRuleValue(GameRules.DO_SEASONAL_GROWTH)) - || world.getBlockId(x, y + 1, z) != 0 - || world.getSeasonManager().getCurrentSeason() == null - || !world.getSeasonManager().getCurrentSeason().growFlowers - || rand.nextInt(256) != 0 - ) return; - int flowerID = determineFlower(world,x,y,z,rand); - world.setBlockWithNotify(x, y + 1, z, flowerID); - } - - private int determineFlower(World world, int x,int y, int z, Random rand){ - int r = rand.nextInt(400); - if (r < 26) { - return Blocks.FLOWER_RED.id(); - } - if (r < 41) { - return Blocks.FLOWER_YELLOW.id(); - } - if (r < 60) { - Biome biome = world.getBlockBiome(x, y + 1, z); - if (biome == Biomes.OVERWORLD_BIRCH_FOREST || biome == Biomes.OVERWORLD_SEASONAL_FOREST) { - return Blocks.FLOWER_PINK.id(); - } - if (biome == Biomes.OVERWORLD_MEADOW || biome == Biomes.OVERWORLD_BOREAL_FOREST || biome == Biomes.OVERWORLD_SHRUBLAND) { - return Blocks.FLOWER_PURPLE.id(); - } - if (biome == Biomes.OVERWORLD_FOREST || biome == Biomes.OVERWORLD_SWAMPLAND || biome == Biomes.OVERWORLD_RAINFOREST || biome == Biomes.OVERWORLD_CAATINGA) { - return Blocks.FLOWER_LIGHT_BLUE.id(); - } - } - if (rand.nextInt(8) == 0) { - return Blocks.TALLGRASS_FERN.id(); - } - return Blocks.TALLGRASS.id(); + @WrapOperation( + method = "updateTick", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/core/world/World;getBlockLightValue(III)I", + ordinal = 2 + ) + ) + public int allowGrassUpdates(World instance, int x, int y, int z, Operation original){ + if(Boolean.TRUE.equals(instance.getGameRuleValue(CommandlyMod.GRASS_SPREADING))){ + return original.call(instance, x, y, z); + } + return 0; } } diff --git a/src/main/java/redart15/commandly/mixins/mixin/StopMossSpreading.java b/src/main/java/redart15/commandly/mixins/mixin/StopMossSpreading.java index 3dfce46..4821d14 100644 --- a/src/main/java/redart15/commandly/mixins/mixin/StopMossSpreading.java +++ b/src/main/java/redart15/commandly/mixins/mixin/StopMossSpreading.java @@ -1,5 +1,7 @@ package redart15.commandly.mixins.mixin; +import com.llamalad7.mixinextras.injector.ModifyReturnValue; +import com.llamalad7.mixinextras.sugar.Local; import net.minecraft.core.block.BlockLogicMoss; import net.minecraft.core.world.World; import org.spongepowered.asm.mixin.Mixin; @@ -11,10 +13,11 @@ @Mixin(value = BlockLogicMoss.class, remap = false) public abstract class StopMossSpreading { - @Inject(method = "canMossSpread(Lnet/minecraft/core/world/World;III)Z", at=@At("HEAD"), cancellable = true) - public void allowedMossSpread(World world, int x, int y, int z, CallbackInfoReturnable cir){ - if(!world.isClientSide && !world.getGameRuleValue(CommandlyMod.MOSS_SPREADING)){ - cir.setReturnValue(false); + @ModifyReturnValue(method = "canMossSpread", at = @At("RETURN")) + public boolean mossSpread(boolean original, World world){ + if(Boolean.TRUE.equals(world.getGameRuleValue(CommandlyMod.MOSS_SPREADING))){ + return original; } + return false; } } diff --git a/src/main/java/redart15/commandly/mixins/treecapitator/SmartTreeCapitator.java b/src/main/java/redart15/commandly/mixins/treecapitator/SmartTreeCapitator.java index 1cc50e4..3d3206c 100644 --- a/src/main/java/redart15/commandly/mixins/treecapitator/SmartTreeCapitator.java +++ b/src/main/java/redart15/commandly/mixins/treecapitator/SmartTreeCapitator.java @@ -13,8 +13,8 @@ import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; +import redart15.commandly.CommandlyMod; -import static redart15.commandly.CommandlyMod.MASK; @Mixin(value = TreecapitatorHelper.class, remap = false) public abstract class SmartTreeCapitator { @@ -30,7 +30,7 @@ public boolean isSmartLog( TreecapitatorHelper asThis = (TreecapitatorHelper) (Object) this; ChunkPosition p = asThis.basePosition; int metadata = asThis.world.getBlockMetadata(p.x, p.y, p.z); - return block != null && !(block.getLogic() instanceof IPaintable) && (metadata >> MASK) == 0 && original.call(instance, block); + return block != null && !(block.getLogic() instanceof IPaintable) && (metadata >> CommandlyMod.getMask()) == 0 && original.call(instance, block); } @@ -52,6 +52,6 @@ private int getBlockID_addLogsAroundBlock(World instance, int x, int y, int z, O @Unique private static boolean isSmartTreecapitator(@NotNull BlockLogic logic, int metadata) { - return logic instanceof IPaintable || (metadata >> MASK) == 1; + return logic instanceof IPaintable || (metadata >> CommandlyMod.getMask()) == 1; } } diff --git a/src/main/java/redart15/commandly/veincapitator/OreGroups.java b/src/main/java/redart15/commandly/veincapitator/OreGroups.java index d71f5b4..f463250 100644 --- a/src/main/java/redart15/commandly/veincapitator/OreGroups.java +++ b/src/main/java/redart15/commandly/veincapitator/OreGroups.java @@ -7,90 +7,123 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import java.util.Set; /// Ore -> Group -> List of Group members public class OreGroups { - public static final OreGroups instance = new OreGroups(); - public Map> GROUP_TO_BLOCKS = new HashMap<>(); - public Map BLOCKS_TO_GROUP = new HashMap<>(); - - public static void init(){} + protected static OreGroups instance; + protected static final Map> GROUP_TO_BLOCKS = new HashMap<>(); + protected static final Map BLOCKS_TO_GROUP = new HashMap<>(); + + public static class OreGroupBuilder { + private final String oreGroupName; + + private OreGroupBuilder(String oreGroupName) { + this.oreGroupName = oreGroupName; + } + + /** + * @implNote Returns a builder to allow chain add. + * */ + public static OreGroupBuilder register(String oreGroup) { + return new OreGroupBuilder(oreGroup); + } + + /** + * @implNote Add ore and its variation to a lookup table. + * */ + public OreGroupBuilder addOre(Block block) { + instance.addOreGroup(this.oreGroupName, block); + return this; + } + } - protected OreGroups() { - this.register(); + public static void init() { + if(instance == null){ + instance = new OreGroups(); + } + instance.load(); } - private void register() { - this.addOreGroups("coal", Blocks.ORE_COAL_BASALT); - this.addOreGroups("coal", Blocks.ORE_COAL_GRANITE); - this.addOreGroups("coal", Blocks.ORE_COAL_LIMESTONE); - this.addOreGroups("coal", Blocks.ORE_COAL_STONE); - this.addOreGroups("coal", Blocks.ORE_COAL_PERMAFROST); - - this.addOreGroups("diamond", Blocks.ORE_DIAMOND_STONE); - this.addOreGroups("diamond",Blocks.ORE_DIAMOND_BASALT); - this.addOreGroups("diamond",Blocks.ORE_DIAMOND_GRANITE); - this.addOreGroups("diamond",Blocks.ORE_DIAMOND_LIMESTONE); - this.addOreGroups("diamond", Blocks.ORE_DIAMOND_PERMAFROST); - - this.addOreGroups("gold",Blocks.ORE_GOLD_STONE); - this.addOreGroups("gold",Blocks.ORE_GOLD_BASALT); - this.addOreGroups("gold",Blocks.ORE_GOLD_GRANITE); - this.addOreGroups("gold",Blocks.ORE_GOLD_LIMESTONE); - this.addOreGroups("gold", Blocks.ORE_GOLD_PERMAFROST); - - this.addOreGroups("iron",Blocks.ORE_IRON_STONE); - this.addOreGroups("iron",Blocks.ORE_IRON_BASALT); - this.addOreGroups("iron",Blocks.ORE_IRON_GRANITE); - this.addOreGroups("iron",Blocks.ORE_IRON_LIMESTONE); - this.addOreGroups("iron",Blocks.ORE_IRON_PERMAFROST); - - this.addOreGroups("nether_coal",Blocks.ORE_NETHERCOAL_NETHERRACK); - - this.addOreGroups("lapis",Blocks.ORE_LAPIS_STONE); - this.addOreGroups("lapis",Blocks.ORE_LAPIS_BASALT); - this.addOreGroups("lapis",Blocks.ORE_LAPIS_GRANITE); - this.addOreGroups("lapis",Blocks.ORE_LAPIS_LIMESTONE); - this.addOreGroups("lapis",Blocks.ORE_LAPIS_PERMAFROST); - - this.addOreGroups("redstone",Blocks.ORE_REDSTONE_STONE); - this.addOreGroups("redstone",Blocks.ORE_REDSTONE_BASALT); - this.addOreGroups("redstone",Blocks.ORE_REDSTONE_GRANITE); - this.addOreGroups("redstone",Blocks.ORE_REDSTONE_LIMESTONE); - this.addOreGroups("redstone",Blocks.ORE_REDSTONE_PERMAFROST); - - this.addOreGroups("redstone",Blocks.ORE_REDSTONE_GLOWING_STONE); - this.addOreGroups("redstone",Blocks.ORE_REDSTONE_GLOWING_BASALT); - this.addOreGroups("redstone",Blocks.ORE_REDSTONE_GLOWING_GRANITE); - this.addOreGroups("redstone",Blocks.ORE_REDSTONE_GLOWING_LIMESTONE); - this.addOreGroups("redstone",Blocks.ORE_REDSTONE_GLOWING_PERMAFROST); + public static OreGroups getInstance(){ + return instance; } - public boolean addOreGroups(String groupName, Block block) { - HashSet group_to_block_set = this.GROUP_TO_BLOCKS.computeIfAbsent(groupName, key -> new HashSet<>()); - if(!group_to_block_set.add(block.namespaceId())) return false; - this.BLOCKS_TO_GROUP.put(block.namespaceId(), groupName); - block.withTags(OreTags.ORE); - return true; + public static OreGroupBuilder register(String string){ + return new OreGroupBuilder(string); } - public String getOreGroupName(Block block){ - return this.BLOCKS_TO_GROUP.getOrDefault(block.namespaceId(), ""); + private OreGroups() {} + + private void load() { + OreGroups.register("coal") + .addOre(Blocks.ORE_COAL_BASALT) + .addOre(Blocks.ORE_COAL_GRANITE) + .addOre(Blocks.ORE_COAL_LIMESTONE) + .addOre(Blocks.ORE_COAL_STONE) + .addOre(Blocks.ORE_COAL_PERMAFROST); + + OreGroups.register("diamond") + .addOre(Blocks.ORE_DIAMOND_STONE) + .addOre(Blocks.ORE_DIAMOND_BASALT) + .addOre(Blocks.ORE_DIAMOND_GRANITE) + .addOre(Blocks.ORE_DIAMOND_LIMESTONE) + .addOre(Blocks.ORE_DIAMOND_PERMAFROST); + + OreGroups.register("gold") + .addOre(Blocks.ORE_GOLD_STONE) + .addOre(Blocks.ORE_GOLD_BASALT) + .addOre(Blocks.ORE_GOLD_GRANITE) + .addOre(Blocks.ORE_GOLD_LIMESTONE) + .addOre(Blocks.ORE_GOLD_PERMAFROST); + + OreGroups.register("iron") + .addOre(Blocks.ORE_IRON_STONE) + .addOre(Blocks.ORE_IRON_BASALT) + .addOre(Blocks.ORE_IRON_GRANITE) + .addOre(Blocks.ORE_IRON_LIMESTONE) + .addOre(Blocks.ORE_IRON_PERMAFROST); + + OreGroups.register("lapis") + .addOre(Blocks.ORE_LAPIS_STONE) + .addOre(Blocks.ORE_LAPIS_BASALT) + .addOre(Blocks.ORE_LAPIS_GRANITE) + .addOre(Blocks.ORE_LAPIS_LIMESTONE) + .addOre(Blocks.ORE_LAPIS_PERMAFROST); + + OreGroups.register("nether_coal") + .addOre(Blocks.ORE_NETHERCOAL_NETHERRACK); + + OreGroups.register("lapis") + .addOre(Blocks.ORE_REDSTONE_STONE) + .addOre(Blocks.ORE_REDSTONE_BASALT) + .addOre(Blocks.ORE_REDSTONE_GRANITE) + .addOre(Blocks.ORE_REDSTONE_LIMESTONE) + .addOre(Blocks.ORE_REDSTONE_PERMAFROST) + .addOre(Blocks.ORE_REDSTONE_GLOWING_STONE) + .addOre(Blocks.ORE_REDSTONE_GLOWING_BASALT) + .addOre(Blocks.ORE_REDSTONE_GLOWING_GRANITE) + .addOre(Blocks.ORE_REDSTONE_GLOWING_LIMESTONE) + .addOre(Blocks.ORE_REDSTONE_GLOWING_PERMAFROST); } - public HashSet getOreGroup(String name){ - return this.GROUP_TO_BLOCKS.getOrDefault(name, new HashSet<>()); + private boolean addOreGroup(String groupName, Block block) { + HashSet groupToBlockSet = GROUP_TO_BLOCKS.computeIfAbsent(groupName, key -> new HashSet<>()); + if (!groupToBlockSet.add(block.namespaceId())) return false; + BLOCKS_TO_GROUP.put(block.namespaceId(), groupName); + block.withTags(OreTags.ORE); + return true; } - public HashSet getOreGroupFromMember(Block block){ - return getOreGroup(getOreGroupName(block)); + public String getOreGroupName(Block block) { + return BLOCKS_TO_GROUP.getOrDefault(block.namespaceId(), ""); } - public Map> getGROUP_TO_BLOCKS(){ - return this.GROUP_TO_BLOCKS; + public Set getOreGroup(String name) { + return GROUP_TO_BLOCKS.getOrDefault(name, new HashSet<>()); } - public Map getBLOCKS_TO_GROUP(){ - return this.BLOCKS_TO_GROUP; + public Set getOreGroupFromMember(Block block) { + return getOreGroup(getOreGroupName(block)); } } diff --git a/src/main/java/redart15/commandly/veincapitator/PickAxeRegister.java b/src/main/java/redart15/commandly/veincapitator/PickAxeRegister.java index 2d8c2d7..ae81538 100644 --- a/src/main/java/redart15/commandly/veincapitator/PickAxeRegister.java +++ b/src/main/java/redart15/commandly/veincapitator/PickAxeRegister.java @@ -3,20 +3,42 @@ import net.minecraft.core.item.Item; import net.minecraft.core.item.material.ToolMaterial; import net.minecraft.core.item.tool.ItemTool; +import org.jetbrains.annotations.NotNull; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; public class PickAxeRegister { - protected static final Map toolSet = new HashMap(); + protected static PickAxeRegister instance; + protected static final Map toolSet = new HashMap<>(); + public static void init() { + if (instance == null) { + instance = new PickAxeRegister(); + } + instance.load(); + } + + private void load() { + // nothign to load + } + + protected PickAxeRegister() { + } + + public static PickAxeRegister getInstance() { + return instance; + } public static boolean containsID(int id) { return toolSet.containsKey(id); } - public static boolean addPickaxe(Item pickaxe) { + public static boolean register(@NotNull Item pickaxe) { + if (pickaxe instanceof ItemTool) { + toolSet.put(pickaxe.id, ((ItemTool) pickaxe).getMaterial()); + } try { Method method = pickaxe.getClass().getMethod("getMaterial"); ToolMaterial toolMaterial = (ToolMaterial) method.invoke(pickaxe); @@ -26,26 +48,31 @@ public static boolean addPickaxe(Item pickaxe) { return false; } catch (Exception e) { e.printStackTrace(); - return false; } + return true; + } + + public static boolean register(@NotNull Item pickaxe, @NotNull ToolMaterial material) { + toolSet.put(pickaxe.id, material); + return true; } - public static ToolMaterial getMaterial(Item item){ - if(item instanceof ItemTool){ + public static ToolMaterial getMaterial(Item item) { + if (item instanceof ItemTool) { return ((ItemTool) item).getMaterial(); } return PickAxeRegister.getMaterial(item.id); } - private static ToolMaterial getMaterial(int id){ + private static ToolMaterial getMaterial(int id) { return toolSet.getOrDefault(id, ToolMaterial.wood); } - public static int getMiningLevel(Item item){ + public static int getMiningLevel(Item item) { return PickAxeRegister.getMaterial(item).getMiningLevel(); } - private static int getMiningLevel(int id){ + private static int getMiningLevel(int id) { return PickAxeRegister.getMaterial(id).getMiningLevel(); } diff --git a/src/main/java/redart15/commandly/veincapitator/VeinMining.java b/src/main/java/redart15/commandly/veincapitator/VeinMining.java index 0b33c37..1d97ca8 100644 --- a/src/main/java/redart15/commandly/veincapitator/VeinMining.java +++ b/src/main/java/redart15/commandly/veincapitator/VeinMining.java @@ -31,11 +31,10 @@ public class VeinMining { private final ItemStack tool; private final ChunkPosition point; private final Player player; - - private Tag> miningTag = BlockTags.MINEABLE_BY_PICKAXE; + private Set>> miningTags = new HashSet<>(); private int radius; - private HashSet miningGroup; + private Set miningGroup; private boolean onlyThisID = false; private ItemList clumpingList; private EnumDropCause dropCause; @@ -47,7 +46,7 @@ public VeinMining(World world, ItemStack itemStack, int x, int y, int z, Player this.point = new ChunkPosition(x, y, z); this.player = player; this.radius = 1; - + this.miningTags.add(BlockTags.MINEABLE_BY_PICKAXE); ToolMaterial material = PickAxeRegister.getMaterial(tool.getItem()); this.dropCause = material.isSilkTouch() ? EnumDropCause.SILK_TOUCH : EnumDropCause.PROPER_TOOL; } @@ -61,10 +60,13 @@ public VeinMining setDropCause(EnumDropCause dropCause) { return this; } - public VeinMining setMiningTag(Tag> mininTag) { - this.miningTag = mininTag; - return this; - } + @SafeVarargs + public final VeinMining setMiningTags(Tag>... miningTags) { + if (miningTags.length > 0) { + this.miningTags = new HashSet<>(Arrays.asList(miningTags)); + } + return this; + } public VeinMining setRadius(int radius) { this.radius = radius; @@ -111,9 +113,9 @@ private boolean canBeVeinMined(Block block) { if (theBlock == null || theBlock.id() != block.id()) { return false; } - if (!block.hasTag(miningTag)) { - return false; - } + if (!this.hasTag(block)) { + return false; + } if (this.tool == null) { return false; @@ -128,6 +130,16 @@ private boolean canBeVeinMined(Block block) { return block.hasTag(ORE) || this.languageKeyOre(block); } + + private boolean hasTag(Block block) { + for(Tag> tag : this.miningTags){ + if(block.hasTag(tag)){ + return true; + } + } + return false; + } + public static boolean canBeVeinMinedCommand(@NotNull Block block) { return (block.hasTag(ORE) || languageKeyOreCheck(block)) && !(block.getLogic() instanceof IPaintable); } @@ -136,7 +148,7 @@ private static boolean isSmartMiner(@NotNull Block block, int metadata) { if(!CommandlyConfig.SMART_VEINMINER){ return false; } - return block.getLogic() instanceof IPaintable || (metadata >> CommandlyMod.MASK) == 1; + return block.getLogic() instanceof IPaintable || (metadata >> CommandlyMod.getMask()) == 1; } private boolean languageKeyOre(Block block) { @@ -162,7 +174,7 @@ private static boolean languageKeyOreCheck(Block block) { return false; } - private HashSet getGroup(Block block) { + private Set getGroup(Block block) { if (onlyThisID) { this.onlyThisID = false; HashSet set = new HashSet<>(); @@ -238,24 +250,9 @@ protected void dropItems(ItemStack[] items, ChunkPosition pos) { private ItemStack[] getBreakResult(@NotNull Block block, World world, EnumDropCause dropCause, int x, int y, int z, int meta, TileEntity tileEntity) { ItemStack[] result = block.getBreakResult(world, dropCause, x, y, z, meta, tileEntity); - if (!this.canGetAdditionalBreakResults(block, tool.getItem(), meta)) { - return result; - } return this.getAdditionalBreakResult(world, result, meta, block); } - private boolean canGetAdditionalBreakResults(Block block, Item tool, int meta) { - try { - BlockLogic logic = block.getLogic(); - Method method = logic.getClass().getMethod("canGetAdditionalBreakResult", Item.class, Integer.class); - return (Boolean) method.invoke(logic, tool, meta); - } catch (NoSuchMethodException e) { - return false; - } catch (IllegalAccessException | InvocationTargetException e) { - throw new RuntimeException(e); - } - } - private ItemStack[] getAdditionalBreakResult(World world, ItemStack[] result, int meta, Block block) { try { BlockLogic logic = block.getLogic();