From 80f9a70553d0c53819a95982f24a80a4f72ce88e Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Sat, 18 Apr 2020 19:44:54 +0200 Subject: Fixed BucketBehavior, added DyeItembehavior Dye in dispenser pointing to a shulkerbox will change the color of the shulkerbox --- .../re/jag/parquet/BucketDispenserBehavior.java | 26 +++++++--- .../re/jag/parquet/DyeItemDispenserBehavior.java | 60 ++++++++++++++++++++++ src/main/java/re/jag/parquet/Parquet.java | 8 ++- 3 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 src/main/java/re/jag/parquet/DyeItemDispenserBehavior.java (limited to 'src/main/java') diff --git a/src/main/java/re/jag/parquet/BucketDispenserBehavior.java b/src/main/java/re/jag/parquet/BucketDispenserBehavior.java index 0dabf5a..4de7495 100644 --- a/src/main/java/re/jag/parquet/BucketDispenserBehavior.java +++ b/src/main/java/re/jag/parquet/BucketDispenserBehavior.java @@ -6,7 +6,9 @@ import net.minecraft.block.CauldronBlock; import net.minecraft.block.DispenserBlock; import net.minecraft.block.FluidDrainable; import net.minecraft.block.dispenser.FallibleItemDispenserBehavior; +import net.minecraft.block.entity.DispenserBlockEntity; import net.minecraft.fluid.Fluid; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.util.math.BlockPointer; @@ -18,27 +20,35 @@ public class BucketDispenserBehavior extends FallibleItemDispenserBehavior { public ItemStack dispenseSilently(BlockPointer pointer, ItemStack stack) { this.success = false; World world = pointer.getWorld(); + Item item; BlockPos block_pos = pointer.getBlockPos().offset((Direction)pointer.getBlockState().get(DispenserBlock.FACING)); BlockState state = world.getBlockState(block_pos); Block block = state.getBlock(); if (block instanceof FluidDrainable) { Fluid fluid = ((FluidDrainable)block).tryDrainFluid(world, block_pos, state); - //Mojang, why are you here? if (!(fluid instanceof net.minecraft.fluid.BaseFluid)) { return super.dispenseSilently(pointer, stack); } - //TODO if not work, add item = ... - fluid.getBucketItem(); - } - if (block instanceof CauldronBlock) { + + item = fluid.getBucketItem(); + } else if (block instanceof CauldronBlock) { if(state.get(CauldronBlock.LEVEL) >= 3) { ((CauldronBlock)block).setLevel(world, block_pos, state, 0); this.success = true; - return new ItemStack(Items.WATER_BUCKET, 1); + item = Items.WATER_BUCKET; + } else { + return stack; } - return stack; + } else { + return super.dispenseSilently(pointer, stack); } - return super.dispenseSilently(pointer, stack); + + stack.decrement(1); + if (stack.isEmpty()) + return new ItemStack(item); + if (((DispenserBlockEntity)pointer.getBlockEntity()).addToFirstFreeSlot(new ItemStack(item)) < 0) + this.dispense(pointer, new ItemStack(item)); + return stack; } } diff --git a/src/main/java/re/jag/parquet/DyeItemDispenserBehavior.java b/src/main/java/re/jag/parquet/DyeItemDispenserBehavior.java new file mode 100644 index 0000000..8bfb779 --- /dev/null +++ b/src/main/java/re/jag/parquet/DyeItemDispenserBehavior.java @@ -0,0 +1,60 @@ +package re.jag.parquet; + +import java.util.List; + +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; +import net.minecraft.block.DispenserBlock; +import net.minecraft.block.ShulkerBoxBlock; +import net.minecraft.block.dispenser.FallibleItemDispenserBehavior; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.ShulkerBoxBlockEntity; +import net.minecraft.item.AutomaticItemPlacementContext; +import net.minecraft.item.BlockItem; +import net.minecraft.item.DyeItem; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.ActionResult; +import net.minecraft.util.math.BlockPointer; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; +import net.minecraft.world.World; + +public class DyeItemDispenserBehavior extends FallibleItemDispenserBehavior{ + public ItemStack dispenseSilently(BlockPointer pointer, ItemStack stack) { + this.success = false; + World world = pointer.getWorld(); + BlockPos block_pos = pointer.getBlockPos().offset((Direction)pointer.getBlockState().get(DispenserBlock.FACING)); + BlockState state = world.getBlockState(block_pos); + Block block = state.getBlock(); + Item item = stack.getItem(); + + if (block instanceof ShulkerBoxBlock && item instanceof DyeItem && world instanceof ServerWorld) { + BlockEntity block_entity = world.getBlockEntity(block_pos); + List dropped_stacks = ShulkerBoxBlock.getDroppedStacks(state, (ServerWorld)world, block_pos, block_entity); + + if (dropped_stacks.size() == 1) { + ItemStack dropped_stack = dropped_stacks.get(0); + ItemStack new_stack = new ItemStack(ShulkerBoxBlock.get(((DyeItem)item).getColor())); + + if (dropped_stack.hasTag()) { + new_stack.setTag(dropped_stack.getTag().copy()); + } + + world.setBlockState(block_pos, Blocks.AIR.getDefaultState()); + + Direction direction = (Direction)pointer.getBlockState().get(DispenserBlock.FACING); + Direction direction2 = pointer.getWorld().isAir(block_pos.down()) ? direction : Direction.UP; + this.success = (((BlockItem)new_stack.getItem()).place(new AutomaticItemPlacementContext(pointer.getWorld(), block_pos, direction, new_stack, direction2)) == ActionResult.SUCCESS); + stack.decrement(1); + } + return stack; + } + + this.success = true; + return super.dispenseSilently(pointer, stack); + } + +} diff --git a/src/main/java/re/jag/parquet/Parquet.java b/src/main/java/re/jag/parquet/Parquet.java index fc8bcaa..dd63220 100644 --- a/src/main/java/re/jag/parquet/Parquet.java +++ b/src/main/java/re/jag/parquet/Parquet.java @@ -6,7 +6,7 @@ import net.fabricmc.api.ModInitializer; import net.minecraft.block.Blocks; import net.minecraft.block.DispenserBlock; import net.minecraft.block.ShulkerBoxBlock; -import net.minecraft.block.dispenser.BlockPlacementDispenserBehavior; +import net.minecraft.item.DyeItem; import net.minecraft.item.Items; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.util.DyeColor; @@ -28,11 +28,17 @@ public class Parquet implements ModInitializer { public static void registerCustomDispenserBehavior() { //TODO Maybe rewrite Cauldron onUse to not only acccept players? + //refactor CauldronBlock onUse + + //This is a rather hacky implementation DispenserBlock.registerBehavior(Blocks.SHULKER_BOX.asItem(), new ShulkerPlacementDispenserBehavior()); for (DyeColor dye_color : DyeColor.values()) { DispenserBlock.registerBehavior(ShulkerBoxBlock.get(dye_color).asItem(), new ShulkerPlacementDispenserBehavior()); + + DispenserBlock.registerBehavior(DyeItem.byColor(dye_color).asItem(), new DyeItemDispenserBehavior()); } + DispenserBlock.registerBehavior(Items.WATER_BUCKET, new WaterBucketDispenserBehavior()); DispenserBlock.registerBehavior(Items.BUCKET, new BucketDispenserBehavior()); DispenserBlock.registerBehavior(Items.GLASS_BOTTLE, new GlassBottleDispenserBehavior()); -- cgit v1.2.3