diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/QuickieFabric.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/QuickieFabric.java index cbf4db3..37c1920 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/QuickieFabric.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/QuickieFabric.java @@ -13,6 +13,8 @@ public class QuickieFabric implements ModInitializer { @Override public void onInitialize() { RegistryManager.registerItems(); + RegistryManager.registerTools(); + RegistryManager.registerEvents(); } } diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/event/BlockBreakMixin.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/event/BlockBreakMixin.java new file mode 100644 index 0000000..a79a257 --- /dev/null +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/event/BlockBreakMixin.java @@ -0,0 +1,29 @@ +package de.jottyfan.minecraft.quickiefabric.event; + +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 net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.util.ActionResult; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +/** + * + * @author jotty + * + */ +@Mixin(Block.class) +public class BlockBreakMixin { + @Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;onBreak(net/minecraft/world/World;net/minecraft/util/math/BlockPos;net/minecraft/block/BlockState;net/minecraft/entity/player/PlayerEntity)V"), method = "interactOnBreak", cancellable = true) + private void interactOnBreak(final World world, final BlockPos blockPos, final BlockState blockState, final PlayerEntity player, final CallbackInfoReturnable info) { + ActionResult result = BreakBlockCallback.EVENT.invoker().interact(world, blockPos, blockState, player); + if (result == ActionResult.FAIL) { + info.cancel(); + } + } +} diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/event/BreakBlockCallback.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/event/BreakBlockCallback.java new file mode 100644 index 0000000..2c13bd1 --- /dev/null +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/event/BreakBlockCallback.java @@ -0,0 +1,29 @@ +package de.jottyfan.minecraft.quickiefabric.event; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.block.BlockState; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.util.ActionResult; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +/** + * + * @author jotty + * + */ +public interface BreakBlockCallback { + Event EVENT = EventFactory.createArrayBacked(BreakBlockCallback.class, + (listeners) -> (world, blockPos, blockState, player) -> { + for (BreakBlockCallback listener : listeners) { + ActionResult result = listener.interact(world, blockPos, blockState, player); + if (result != ActionResult.PASS) { + return result; + } + } + return ActionResult.PASS; + }); + + ActionResult interact(World world, BlockPos blockPos, BlockState blockState, PlayerEntity player); +} diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/event/EventBlockBreak.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/event/EventBlockBreak.java new file mode 100644 index 0000000..afc7d48 --- /dev/null +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/event/EventBlockBreak.java @@ -0,0 +1,133 @@ +package de.jottyfan.minecraft.quickiefabric.event; + +/** + * + * @author jotty + * + */ +public class EventBlockBreak { + + private enum BlockBreakDirection { + UPWARDS, ALL; + } + + +// /** +// * TODO: use AttackBlockCallback instead? +// * @param event +// */ +// @SubscribeEvent +// public void doBreakBlock(BlockEvent.BreakEvent event) { +// PlayerEntity player = event.getPlayer(); +// ItemStack mainHandItemStack = player.getHeldItemMainhand(); +// if (mainHandItemStack != null) { +// Item item = mainHandItemStack.getItem(); +// if (item instanceof ToolRangeable) { +// CompoundNBT nbt = mainHandItemStack.getTag(); +// Integer level = nbt != null ? nbt.getInt("level") : 0; +// ToolRangeable tool = (ToolRangeable) item; +// BlockPos pos = event.getPos(); +// World world = event.getWorld().getWorld(); +// BlockState blockState = world.getBlockState(pos); +// Block block = blockState.getBlock(); +// handleRangeableTools(tool, level, world, block, pos, player); +// } +// } +// } +// +// /** +// * hande the rangeable tools break event +// * +// * @param tool +// * the tool that has been used +// * @param world +// * the world +// * @param block +// * the block to break +// * @param pos +// * the position of the current block +// * @param player +// * the current player +// */ +// private void handleRangeableTools(ToolRangeable tool, Integer level, World world, Block currentBlock, BlockPos pos, +// PlayerEntity player) { +// List validBlocks = tool.getBlockList(currentBlock); +// HarvestRange range = tool.getRange(); +// if (range != null) { +// range = range.addXYZ(level); +// } +// if (RegistryManager.AXE_SPEEDPOWDER.equals(tool)) { +// breakBlockRecursive(new ArrayList<>(), world, validBlocks, pos, tool, range, BlockBreakDirection.UPWARDS, player); +// } else if (RegistryManager.PICKAXE_SPEEDPOWDER.equals(tool)) { +// breakBlockRecursive(new ArrayList<>(), world, validBlocks, pos, tool, range, BlockBreakDirection.ALL, player); +// } else if (RegistryManager.SHOVEL_SPEEDPOWDER.equals(tool)) { +// breakBlockRecursive(new ArrayList<>(), world, validBlocks, pos, tool, range, BlockBreakDirection.ALL, player); +// } +// } +// +// /** +// * break block recursively; +// * +// * @param visitedBlocks +// * the positions of visited blocks +// * @param world +// * the world +// * @param validBlocks +// * the blocks to break +// * @param tool +// * the tool used +// * @param range +// * the range left over +// * @param pos +// * the position +// * @param blockBreakDirection +// * the direction for the recursive call +// * @param player +// * the player +// */ +// private void breakBlockRecursive(List visitedBlocks, World world, List validBlocks, BlockPos pos, +// ToolRangeable tool, HarvestRange range, BlockBreakDirection blockBreakDirection, PlayerEntity player) { +// if (visitedBlocks.contains(pos.toString())) { +// return; // reduce loops +// } else if (validBlocks == null) { +// return; // reduce loops +// } else { +// visitedBlocks.add(pos.toString()); +// } +// BlockState blockState = world.getBlockState(pos); +// if (tool.canBreakNeigbbors(blockState)) { +// Block currentBlock = blockState.getBlock(); +// if (validBlocks.contains(currentBlock)) { +// world.setBlockState(pos, Blocks.AIR.getDefaultState()); +// Block.spawnDrops(blockState, world, pos); +// if (range == null || range.getxRange() > 1 || range.getyRange() > 1 || range.getzRange() > 1) { +// HarvestRange nextRadius = range == null ? null : range.addXYZ(-1); +// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.north(), tool, nextRadius, blockBreakDirection, +// player); +// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.north().east(), tool, nextRadius, +// blockBreakDirection, player); +// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.north().west(), tool, nextRadius, +// blockBreakDirection, player); +// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.south(), tool, nextRadius, blockBreakDirection, +// player); +// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.south().east(), tool, nextRadius, +// blockBreakDirection, player); +// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.south().west(), tool, nextRadius, +// blockBreakDirection, player); +// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.east(), tool, nextRadius, blockBreakDirection, +// player); +// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.west(), tool, nextRadius, blockBreakDirection, +// player); +// +// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.up(), tool, nextRadius, blockBreakDirection, +// player); +// +// if (BlockBreakDirection.ALL.equals(blockBreakDirection)) { +// breakBlockRecursive(visitedBlocks, world, validBlocks, pos.down(), tool, nextRadius, blockBreakDirection, +// player); +// } +// } +// } +// } +// } +} diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/init/RegistryManager.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/init/RegistryManager.java index 8cef2bc..775eadf 100644 --- a/src/main/java/de/jottyfan/minecraft/quickiefabric/init/RegistryManager.java +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/init/RegistryManager.java @@ -1,13 +1,14 @@ package de.jottyfan.minecraft.quickiefabric.init; -import de.jottyfan.minecraft.quickiefabric.items.ItemLevelup; -import de.jottyfan.minecraft.quickiefabric.items.ItemPencil; -import de.jottyfan.minecraft.quickiefabric.items.ItemSalpeter; -import de.jottyfan.minecraft.quickiefabric.items.ItemSpeedpowder; -import de.jottyfan.minecraft.quickiefabric.items.ItemSulphor; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import de.jottyfan.minecraft.quickiefabric.event.BreakBlockCallback; +import de.jottyfan.minecraft.quickiefabric.items.Items; +import de.jottyfan.minecraft.quickiefabric.tools.Tools; import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder; import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemStack; +import net.minecraft.util.ActionResult; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; @@ -17,30 +18,41 @@ import net.minecraft.util.registry.Registry; * */ public class RegistryManager { + private static final Logger LOGGER = LogManager.getLogger(RegistryManager.class); private static final String QUICKIEFABRIC = "quickiefabric"; - - public static final ItemSpeedpowder ITEM_SPEEDPOWDER = new ItemSpeedpowder(); - public static final ItemLevelup ITEM_LEVELUP = new ItemLevelup(); - public static final ItemPencil ITEM_PENCIL = new ItemPencil(); - public static final ItemSalpeter ITEM_SALPETER = new ItemSalpeter(); - public static final ItemSulphor ITEM_SULPHOR = new ItemSulphor(); - public static final ItemGroup QUICKIEFABRIC_GROUP = FabricItemGroupBuilder.create(new Identifier(QUICKIEFABRIC, "all")).icon(() -> new ItemStack(ITEM_SPEEDPOWDER)).appendItems(stacks -> { - stacks.add(new ItemStack(ITEM_SALPETER)); - stacks.add(new ItemStack(ITEM_SULPHOR)); - stacks.add(new ItemStack(ITEM_SPEEDPOWDER)); - stacks.add(new ItemStack(ITEM_LEVELUP)); - stacks.add(new ItemStack(ITEM_PENCIL)); + public static final ItemGroup QUICKIEFABRIC_GROUP = FabricItemGroupBuilder.create(new Identifier(QUICKIEFABRIC, "all")).icon(() -> new ItemStack(Items.SPEEDPOWDER)).appendItems(stacks -> { + stacks.add(new ItemStack(Items.SALPETER)); + stacks.add(new ItemStack(Items.SULPHOR)); + stacks.add(new ItemStack(Items.SPEEDPOWDER)); + stacks.add(new ItemStack(Items.LEVELUP)); + stacks.add(new ItemStack(Items.PENCIL)); + stacks.add(new ItemStack(Tools.SPEEDPOWDERAXE)); }).build(); public static final void registerItems() { - - Registry.register(Registry.ITEM, new Identifier(QUICKIEFABRIC, "speedpowder"), ITEM_SPEEDPOWDER); - Registry.register(Registry.ITEM, new Identifier(QUICKIEFABRIC, "levelup"), ITEM_LEVELUP); - Registry.register(Registry.ITEM, new Identifier(QUICKIEFABRIC, "pencil"), ITEM_PENCIL); - Registry.register(Registry.ITEM, new Identifier(QUICKIEFABRIC, "salpeter"), ITEM_SALPETER); - Registry.register(Registry.ITEM, new Identifier(QUICKIEFABRIC, "sulphor"), ITEM_SULPHOR); + LOGGER.debug("registering quickiefabric items"); + Registry.register(Registry.ITEM, new Identifier(QUICKIEFABRIC, "speedpowder"), Items.SPEEDPOWDER); + Registry.register(Registry.ITEM, new Identifier(QUICKIEFABRIC, "levelup"), Items.LEVELUP); + Registry.register(Registry.ITEM, new Identifier(QUICKIEFABRIC, "pencil"), Items.PENCIL); + Registry.register(Registry.ITEM, new Identifier(QUICKIEFABRIC, "salpeter"), Items.SALPETER); + Registry.register(Registry.ITEM, new Identifier(QUICKIEFABRIC, "sulphor"), Items.SULPHOR); } + public static final void registerTools() { + LOGGER.debug("registering quickiefabric tools"); + Registry.register(Registry.ITEM, new Identifier(QUICKIEFABRIC, "speedpowderaxe"), Tools.SPEEDPOWDERAXE); + } + + public static final void registerEvents() { + LOGGER.debug("registering quickiefabric events"); + BreakBlockCallback.EVENT.register((world, blockPos, blockState, player) -> { + // TODO: add code to break the corresponding surroundings also if hand hold the right tool + // return ActionResult.PASS; // if the breaking replaces another event, but this does not appear for the speedpowder tools + + LOGGER.info("player %s broke block %s as %s", player.getName(), blockState.getBlock().getName(), blockPos.toString()); + return ActionResult.SUCCESS; + }); + } } diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/items/Items.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/items/Items.java new file mode 100644 index 0000000..2d8cc16 --- /dev/null +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/items/Items.java @@ -0,0 +1,14 @@ +package de.jottyfan.minecraft.quickiefabric.items; + +/** + * + * @author jotty + * + */ +public class Items { + public static final ItemSpeedpowder SPEEDPOWDER = new ItemSpeedpowder(); + public static final ItemLevelup LEVELUP = new ItemLevelup(); + public static final ItemPencil PENCIL = new ItemPencil(); + public static final ItemSalpeter SALPETER = new ItemSalpeter(); + public static final ItemSulphor SULPHOR = new ItemSulphor(); +} diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/tools/CommonToolCode.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/tools/CommonToolCode.java new file mode 100644 index 0000000..6b68f1d --- /dev/null +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/tools/CommonToolCode.java @@ -0,0 +1,58 @@ +package de.jottyfan.minecraft.quickiefabric.tools; + +import java.util.List; + +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; + +/** + * + * @author jotty + * + */ +public class CommonToolCode { +// public static final void onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) { +// CompoundNBT nbt = playerIn.getHeldItem(handIn).getTag(); +// if (nbt != null) { +// int level = nbt.getInt("level"); +// if (playerIn.isSneaking()) { +// // drop all enhancements into the players inventory if possible +// ItemStack stack = new ItemStack(QuickieItems.ITEM_LEVELUP, level); +// boolean unlevelingerror = false; +// if (!worldIn.isRemote) { +// if (playerIn.addItemStackToInventory(stack)) { +// level = 0; +// } else { +// unlevelingerror = true; +// } +// } +// if (worldIn.isRemote && unlevelingerror) { +// playerIn.sendMessage(new TranslationTextComponent("error.unleveling.inventory.full"), PlayerEntity.getUUID(playerIn.getGameProfile())); +// } +// } else { +// // add all enhancements in players inventory +// if (!worldIn.isRemote) { +// NonNullList main = playerIn.inventory.mainInventory; +// for (ItemStack stack : main) { +// if (stack.getItem().equals(RegistryManager.ITEM_LEVELUP)) { +// level += stack.getCount(); +// stack.setCount(0); +// } +// } +// } +// } +// nbt.putInt("level", level); +// } +// } +// +// public static final void addInformation(ItemStack stack, World worldIn, List tooltip, ITooltipFlag flagIn) { +// if (tooltip != null) { +// CompoundNBT nbt = stack.getTag(); +// if (nbt != null) { +// tooltip.add(new StringTextComponent("level " + nbt.getInt("level"))); +// } else { +// tooltip.add(new StringTextComponent("level 0")); +// } +// } +// } +} diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/tools/HarvestRange.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/tools/HarvestRange.java new file mode 100644 index 0000000..4008bca --- /dev/null +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/tools/HarvestRange.java @@ -0,0 +1,85 @@ +package de.jottyfan.minecraft.quickiefabric.tools; + +import java.io.Serializable; + +/** + * + * @author jotty + * + */ +public class HarvestRange implements Serializable { + private static final long serialVersionUID = 1L; + private int xRange; + private int yRange; + private int zRange; + + public HarvestRange(int xyzRange) { + super(); + this.xRange = xyzRange; + this.yRange = xyzRange; + this.zRange = xyzRange; + } + + public HarvestRange(int xRange, int yRange, int zRange) { + super(); + this.xRange = xRange; + this.yRange = yRange; + this.zRange = zRange; + } + + /** + * add i to x, y and z and return the resulting class as a new one + * + * @param i + * the summand + * @return the new class + */ + public HarvestRange addXYZ(int i) { + return new HarvestRange(xRange + i, yRange + i, zRange + i); + } + + /** + * @return the xRange + */ + public int getxRange() { + return xRange; + } + + /** + * @param xRange + * the xRange to set + */ + public void setxRange(int xRange) { + this.xRange = xRange; + } + + /** + * @return the yRange + */ + public int getyRange() { + return yRange; + } + + /** + * @param yRange + * the yRange to set + */ + public void setyRange(int yRange) { + this.yRange = yRange; + } + + /** + * @return the zRange + */ + public int getzRange() { + return zRange; + } + + /** + * @param zRange + * the zRange to set + */ + public void setzRange(int zRange) { + this.zRange = zRange; + } +} diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/tools/ToolRangeable.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/tools/ToolRangeable.java new file mode 100644 index 0000000..f0485a2 --- /dev/null +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/tools/ToolRangeable.java @@ -0,0 +1,97 @@ +package de.jottyfan.minecraft.quickiefabric.tools; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; + +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; +import net.minecraft.util.Identifier; + +/** + * + * @author jotty + * + */ +public interface ToolRangeable { + public static final Set SHOVEL_EFFECTIVE_ON = Sets.newHashSet(new Block[] { Blocks.GRAVEL, Blocks.SAND, + Blocks.GRASS_BLOCK, Blocks.DIRT, Blocks.CLAY, Blocks.FARMLAND, Blocks.GRASS_PATH, Blocks.RED_SAND, Blocks.SOUL_SAND }); + + public static final List> AXE_BLOCKLISTS = Lists.newArrayList( + Arrays.asList(Blocks.ACACIA_LOG, Blocks.STRIPPED_ACACIA_LOG, Blocks.ACACIA_WOOD, Blocks.STRIPPED_ACACIA_WOOD), + Arrays.asList(Blocks.BIRCH_LOG, Blocks.STRIPPED_BIRCH_LOG, Blocks.BIRCH_WOOD, Blocks.STRIPPED_BIRCH_WOOD), + Arrays.asList(Blocks.DARK_OAK_LOG, Blocks.STRIPPED_DARK_OAK_LOG, Blocks.DARK_OAK_WOOD, + Blocks.STRIPPED_DARK_OAK_WOOD), + Arrays.asList(Blocks.JUNGLE_LOG, Blocks.STRIPPED_JUNGLE_LOG, Blocks.JUNGLE_WOOD, Blocks.STRIPPED_JUNGLE_WOOD), + Arrays.asList(Blocks.OAK_LOG, Blocks.STRIPPED_OAK_LOG, Blocks.OAK_WOOD, Blocks.STRIPPED_OAK_WOOD), + Arrays.asList(Blocks.SPRUCE_LOG, Blocks.STRIPPED_SPRUCE_LOG, Blocks.SPRUCE_WOOD, Blocks.STRIPPED_SPRUCE_WOOD), + Arrays.asList(Blocks.ACACIA_PLANKS), Arrays.asList(Blocks.BIRCH_PLANKS), Arrays.asList(Blocks.DARK_OAK_PLANKS), + Arrays.asList(Blocks.JUNGLE_PLANKS), Arrays.asList(Blocks.OAK_PLANKS), Arrays.asList(Blocks.SPRUCE_PLANKS), + Arrays.asList(Blocks.ACACIA_SLAB), Arrays.asList(Blocks.BIRCH_SLAB), Arrays.asList(Blocks.DARK_OAK_SLAB), + Arrays.asList(Blocks.JUNGLE_SLAB), Arrays.asList(Blocks.OAK_SLAB), Arrays.asList(Blocks.SPRUCE_SLAB), + Arrays.asList(Blocks.ACACIA_STAIRS), Arrays.asList(Blocks.BIRCH_STAIRS), Arrays.asList(Blocks.DARK_OAK_STAIRS), + Arrays.asList(Blocks.JUNGLE_STAIRS), Arrays.asList(Blocks.OAK_STAIRS), Arrays.asList(Blocks.SPRUCE_STAIRS), + Arrays.asList(Blocks.POTTED_BROWN_MUSHROOM, Blocks.BROWN_MUSHROOM_BLOCK, Blocks.BROWN_MUSHROOM, + Blocks.MUSHROOM_STEM), + Arrays.asList(Blocks.POTTED_RED_MUSHROOM, Blocks.RED_MUSHROOM_BLOCK, Blocks.RED_MUSHROOM, Blocks.MUSHROOM_STEM)); + + public static final Set PICKAXE_BLOCKLISTS = Sets.newHashSet(new Block[] {Blocks.GLOWSTONE}); + + public static final Set BIOMESOPLENTY_SHOVEL = Sets + .newHashSet(new Identifier[] { new Identifier("biomesoplenty", "flesh"), + new Identifier("biomesoplenty", "dirt"), new Identifier("biomesoplenty", "grass"), + new Identifier("biomesoplenty", "mud"), new Identifier("biomesoplenty", "white_sand") }); + + public static final Set BIOMESOPLENTY_PICKAXE = Sets + .newHashSet(new Identifier[] { new Identifier("biomesoplenty", "dried_sand") }); + + public static final List> BIOMESOPLENTY_AXE = Lists.newArrayList(generateBOPAxeSet()); + + static List> generateBOPAxeSet() { + List> list = new ArrayList<>(); + String[] bOPLogs = new String[] { "cherry", "dead", "ethereal", "fir", "hellbark", "jacaranda", "magic", "mahogany", + "palm", "redwood", "umbran", "willow" }; + for (String s : bOPLogs) { + Set set = new HashSet<>(); + set.add(new Identifier("biomesoplenty", s + "_log")); + set.add(new Identifier("biomesoplenty", s + "_wood")); + set.add(new Identifier("biomesoplenty", "stripped_" + s + "_log")); + set.add(new Identifier("biomesoplenty", "stripped_" + s + "_wood")); + list.add(set); + list.add(Sets.newHashSet(new Identifier("biomesoplenty", s + "_stairs"))); + list.add(Sets.newHashSet(new Identifier("biomesoplenty", s + "_planks"))); + list.add(Sets.newHashSet(new Identifier("biomesoplenty", s + "_slab"))); + } + return list; + } + + /** + * @return range of blocks to be harvested + */ + public HarvestRange getRange(); + + /** + * check if this block state is one that affects the neighbor blocks to break + * also if they are from the same type + * + * @param blockState + * the block state of the current block + * @return true or false + */ + public boolean canBreakNeigbbors(BlockState blockState); + + /** + * get list of blocks that belong together + * + * @param block + * of the set + * @return the list of blocks or null if not found + */ + public List getBlockList(Block block); +} diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/tools/ToolRangeableAxe.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/tools/ToolRangeableAxe.java new file mode 100644 index 0000000..a93ae78 --- /dev/null +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/tools/ToolRangeableAxe.java @@ -0,0 +1,131 @@ +package de.jottyfan.minecraft.quickiefabric.tools; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map.Entry; +import java.util.Set; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.entity.Entity; +import net.minecraft.entity.ItemEntity; +import net.minecraft.entity.LivingEntity; +import net.minecraft.item.AxeItem; +import net.minecraft.item.ItemStack; +import net.minecraft.item.ItemUsageContext; +import net.minecraft.item.ToolMaterial; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Identifier; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.registry.DefaultedRegistry; +import net.minecraft.util.registry.Registry; +import net.minecraft.util.registry.RegistryKey; +import net.minecraft.world.World; + +/** + * + * @author jotty + * + */ +public class ToolRangeableAxe extends AxeItem implements ToolRangeable { + + private final static Logger LOGGER = LogManager.getLogger(ToolRangeableAxe.class); + + protected ToolRangeableAxe(ToolMaterial material, float attachDamage, float attackSpeedIn, Settings settings) { + super(material, attachDamage, attackSpeedIn, settings); + } + + /** + * generate a block list of the resource locations in set + * + * @param set + * the set to be used as source for the block list + * @return a block list, at least an empty one + */ + private List generateBlockList(Set set) { + List blocks = new ArrayList<>(); + Set rls = new HashSet<>(set); // copy to omit changes on the set + DefaultedRegistry allBlocks = Registry.BLOCK; + Set, Block>> entries = allBlocks.getEntries(); + for (Entry, Block> entry : entries) { + Iterator i = rls.iterator(); + while (i.hasNext()) { + Identifier rl = i.next(); + if (rl.equals(entry.getKey().getValue())) { + blocks.add(entry.getValue()); + i.remove(); // speed up algorithm + } + } + } + return blocks; + } + + /** + * this method only exists because of try to find a way in fabric to break the neighbor blocks also + * + */ +// @Override +// public ItemStack finishUsing(ItemStack stack, World world, LivingEntity user) { +// LOGGER.info("start finishUsing"); +// +// BlockPos blockpos = user.getBlockPos(); +// LOGGER.info("at position %s", blockpos.toString()); +// BlockState blockStateAbove = world.getBlockState(blockpos.up()); +// BlockState blockStateCurrent = world.getBlockState(blockpos); +// LOGGER.info("compare name of %s and %s: %s =?= %s", blockpos.toString(), blockpos.up().toString(), blockStateCurrent.getBlock().getName(), blockStateAbove.getBlock().getName()); +// if (blockStateAbove.getBlock().getName().equals(blockStateCurrent.getBlock().getName())) { +// LOGGER.info("found equal block above"); +// +// // found same block above +// ItemStack droppedStack = new ItemStack(blockStateAbove.getBlock().asItem()); +// +// // TODO: merge all item stacks of all blocks into one to reduce lag +// double x = blockpos.up().getX(); +// double y = blockpos.up().getY(); +// double z = blockpos.up().getZ(); +// ItemEntity itemEntity = new ItemEntity(world, x, y, z, droppedStack); +// +// world.spawnEntity(itemEntity); +// world.removeBlock(blockpos.up(), false); +// } else { +// LOGGER.info("blocks are not equal"); +// } +// return super.finishUsing(stack, world, user); +// } + + @Override + public HarvestRange getRange() { + return null; // no limit + } + + @Override + public float getMiningSpeedMultiplier(ItemStack stack, BlockState state) { + return getBlockList(state.getBlock()) != null ? this.miningSpeed : super.getMiningSpeedMultiplier(stack, state); + } + + @Override + public boolean canBreakNeigbbors(BlockState blockIn) { + return getBlockList(blockIn.getBlock()) != null; + } + + @Override + public List getBlockList(Block block) { + for (List blockList : AXE_BLOCKLISTS) { + if (blockList.contains(block)) { + return blockList; + } + } +// for (Set identifiers : BIOMESOPLENTY_AXE) { +// Identifier blockIdentifier = block.getBlock().getRegistryName(); +// if (identifiers.contains(blockIdentifier)) { +// return generateBlockList(identifiers); +// } +// } + return null; + } +} diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/tools/ToolSpeedpowderAxe.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/tools/ToolSpeedpowderAxe.java new file mode 100644 index 0000000..b66cc4e --- /dev/null +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/tools/ToolSpeedpowderAxe.java @@ -0,0 +1,16 @@ +package de.jottyfan.minecraft.quickiefabric.tools; + +import de.jottyfan.minecraft.quickiefabric.init.RegistryManager; +import net.minecraft.item.Item; +import net.minecraft.item.ToolMaterials; + +/** + * + * @author jotty + * + */ +public class ToolSpeedpowderAxe extends ToolRangeableAxe { + public ToolSpeedpowderAxe() { + super(ToolMaterials.DIAMOND, 4, 2.0f, new Item.Settings().group(RegistryManager.QUICKIEFABRIC_GROUP)); + } +} diff --git a/src/main/java/de/jottyfan/minecraft/quickiefabric/tools/Tools.java b/src/main/java/de/jottyfan/minecraft/quickiefabric/tools/Tools.java new file mode 100644 index 0000000..af7d4d5 --- /dev/null +++ b/src/main/java/de/jottyfan/minecraft/quickiefabric/tools/Tools.java @@ -0,0 +1,10 @@ +package de.jottyfan.minecraft.quickiefabric.tools; + +/** + * + * @author jotty + * + */ +public class Tools { + public static final ToolSpeedpowderAxe SPEEDPOWDERAXE = new ToolSpeedpowderAxe(); +} diff --git a/src/main/resources/assets/quickiefabric/models/item/speedpowderaxe.json b/src/main/resources/assets/quickiefabric/models/item/speedpowderaxe.json new file mode 100644 index 0000000..ced7e04 --- /dev/null +++ b/src/main/resources/assets/quickiefabric/models/item/speedpowderaxe.json @@ -0,0 +1,6 @@ +{ + "parent": "item/wooden_axe", + "textures": { + "layer0": "quickiefabric:item/speedpowderaxe" + } +} \ No newline at end of file diff --git a/src/main/resources/assets/quickiefabric/textures/item/speedpowderaxe.png b/src/main/resources/assets/quickiefabric/textures/item/speedpowderaxe.png new file mode 100644 index 0000000..a65fa58 Binary files /dev/null and b/src/main/resources/assets/quickiefabric/textures/item/speedpowderaxe.png differ diff --git a/src/main/resources/data/quickiefabric/recipes/speedpowderaxe.json b/src/main/resources/data/quickiefabric/recipes/speedpowderaxe.json new file mode 100644 index 0000000..3eb4172 --- /dev/null +++ b/src/main/resources/data/quickiefabric/recipes/speedpowderaxe.json @@ -0,0 +1,20 @@ +{ + "type": "minecraft:crafting_shaped", + "pattern": [ + "ss ", + "s| ", + " | " + ], + "key": { + "s": { + "item": "quickiefabric:speedpowder" + }, + "|": { + "item": "minecraft:stick" + } + }, + "result": { + "item": "quickiefabric:speedpowderaxe", + "count": 1 + } +} \ No newline at end of file diff --git a/src/main/resources/quickiefabric.mixins.json b/src/main/resources/quickiefabric.mixins.json new file mode 100644 index 0000000..73f07bc --- /dev/null +++ b/src/main/resources/quickiefabric.mixins.json @@ -0,0 +1,14 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "de.jottyfan.minecraft.quickiefabric.event", + "compatibilityLevel": "JAVA_8", + "mixins": [ + "BlockBreakMixin" + ], + "client": [ + ], + "injectors": { + "defaultRequire": 1 + } +} \ No newline at end of file