diff --git a/src/main/java/vazkii/botania/client/core/handler/BlockHighlightRenderHandler.java b/src/main/java/vazkii/botania/client/core/handler/BlockHighlightRenderHandler.java index 0df947421..3d0472aca 100644 --- a/src/main/java/vazkii/botania/client/core/handler/BlockHighlightRenderHandler.java +++ b/src/main/java/vazkii/botania/client/core/handler/BlockHighlightRenderHandler.java @@ -36,6 +36,7 @@ import vazkii.botania.common.item.ModItems; import vazkii.botania.common.lib.LibMisc; import java.awt.*; +import java.util.Optional; @Mod.EventBusSubscriber(value = Dist.CLIENT, modid = LibMisc.MOD_ID) public final class BlockHighlightRenderHandler { @@ -54,28 +55,25 @@ public final class BlockHighlightRenderHandler { GlStateManager.enableBlend(); GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); - boundTile: { - if(Botania.proxy.isClientPlayerWearingMonocle() && pos != null && pos.getType() == RayTraceResult.Type.BLOCK) { - BlockPos bPos = ((BlockRayTraceResult) pos).getPos(); + if (Botania.proxy.isClientPlayerWearingMonocle() && pos != null && pos.getType() == RayTraceResult.Type.BLOCK) { + BlockPos bPos = ((BlockRayTraceResult) pos).getPos(); - ItemStack stackHeld = PlayerHelper.getFirstHeldItem(mc.player, ModItems.twigWand); - if(!stackHeld.isEmpty() && ItemTwigWand.getBindMode(stackHeld)) { - BlockPos coords = ItemTwigWand.getBoundTile(stackHeld); - if(coords.getY() != -1) - bPos = coords; - } + ItemStack stackHeld = PlayerHelper.getFirstHeldItem(mc.player, ModItems.twigWand); + if (!stackHeld.isEmpty() && ItemTwigWand.getBindMode(stackHeld)) { + Optional coords = ItemTwigWand.getBindingAttempt(stackHeld); + if (coords.isPresent()) + bPos = coords.get(); + } - TileEntity tile = mc.world.getTileEntity(bPos); - if(!(tile instanceof TileEntitySpecialFlower)) - break boundTile; + TileEntity tile = mc.world.getTileEntity(bPos); + if (tile instanceof TileEntitySpecialFlower) { TileEntitySpecialFlower subtile = (TileEntitySpecialFlower) tile; RadiusDescriptor descriptor = subtile.getRadius(); - if(descriptor == null) - break boundTile; - - if(descriptor.isCircle()) - renderCircle(descriptor.getSubtileCoords(), descriptor.getCircleRadius()); - else renderRectangle(descriptor.getAABB(), true, null, (byte) 32); + if (descriptor != null) { + if (descriptor.isCircle()) + renderCircle(descriptor.getSubtileCoords(), descriptor.getCircleRadius()); + else renderRectangle(descriptor.getAABB(), true, null, (byte) 32); + } } } diff --git a/src/main/java/vazkii/botania/common/block/BlockPistonRelay.java b/src/main/java/vazkii/botania/common/block/BlockPistonRelay.java index 3037344f4..507c24884 100644 --- a/src/main/java/vazkii/botania/common/block/BlockPistonRelay.java +++ b/src/main/java/vazkii/botania/common/block/BlockPistonRelay.java @@ -42,6 +42,9 @@ import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.server.ServerLifecycleHooks; import vazkii.botania.api.wand.IWandable; import vazkii.botania.common.core.handler.ModSounds; +import vazkii.botania.common.item.ItemTwigWand; +import vazkii.botania.common.network.PacketBotaniaEffect; +import vazkii.botania.common.network.PacketHandler; import javax.annotation.Nonnull; import java.util.HashMap; @@ -106,12 +109,22 @@ public class BlockPistonRelay extends BlockMod implements IWandable { return false; if(!player.isSneaking()) { - activeBindingAttempts.put(player.getUniqueID(), GlobalPos.of(world.getDimension().getType(), pos)); - world.playSound(null, pos, ModSounds.ding, SoundCategory.BLOCKS, 0.5F, 1F); + GlobalPos clicked = GlobalPos.of(world.getDimension().getType(), pos); + if(ItemTwigWand.getBindMode(stack)) { + activeBindingAttempts.put(player.getUniqueID(), clicked); + world.playSound(null, pos, ModSounds.ding, SoundCategory.BLOCKS, 0.5F, 1F); + } else { + GlobalPos dest = mappedPositions.get(clicked); + if (dest != null) { + PacketHandler.sendToNearby(world, pos, + new PacketBotaniaEffect(PacketBotaniaEffect.EffectType.PARTICLE_BEAM, + pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, + dest.getPos().getX(), dest.getPos().getY(), dest.getPos().getZ())); + } + } } else { spawnAsEntity(world, pos, new ItemStack(this)); - world.removeBlock(pos, false); - world.playEvent(2001, pos, Block.getStateId(getDefaultState())); + world.destroyBlock(pos, false); } return true; diff --git a/src/main/java/vazkii/botania/common/item/ItemTwigWand.java b/src/main/java/vazkii/botania/common/item/ItemTwigWand.java index cf7723630..c49ae0951 100644 --- a/src/main/java/vazkii/botania/common/item/ItemTwigWand.java +++ b/src/main/java/vazkii/botania/common/item/ItemTwigWand.java @@ -67,6 +67,7 @@ import vazkii.botania.common.network.PacketHandler; import javax.annotation.Nonnull; import java.awt.*; import java.util.List; +import java.util.Optional; public class ItemTwigWand extends ItemMod implements ICoordBoundItem { @@ -83,6 +84,89 @@ public class ItemTwigWand extends ItemMod implements ICoordBoundItem { addPropertyOverride(new ResourceLocation("botania", "bindmode"), (stack, worldIn, entityIn) -> getBindMode(stack) ? 1 : 0); } + private static boolean tryCompleteBinding(BlockPos src, ItemStack stack, ItemUseContext ctx) { + BlockPos dest = ctx.getPos(); + if(!dest.equals(src)) { + setBindingAttempt(stack, UNBOUND_POS); + + TileEntity srcTile = ctx.getWorld().getTileEntity(src); + if (srcTile instanceof IWandBindable) { + if(((IWandBindable) srcTile).bindTo(ctx.getPlayer(), stack, dest, ctx.getFace())) { + doParticleBeamWithOffset(ctx.getWorld(), src, dest); + VanillaPacketDispatcher.dispatchTEToNearbyPlayers(ctx.getWorld(), src); + setBindingAttempt(stack, UNBOUND_POS); + } + return true; + } + } + return false; + } + + private static boolean tryFormEnchanter(ItemUseContext ctx) { + World world = ctx.getWorld(); + BlockPos pos = ctx.getPos(); + Direction.Axis axis = null; + if(TileEnchanter.canEnchanterExist(world, pos, Direction.Axis.X)) + axis = Direction.Axis.X; + else if(TileEnchanter.canEnchanterExist(world, pos, Direction.Axis.Z)) + axis = Direction.Axis.Z; + + if(axis != null) { + if(!world.isRemote) { + world.setBlockState(pos, ModBlocks.enchanter.getDefaultState().with(BotaniaStateProps.ENCHANTER_DIRECTION, axis), 1 | 2); + world.playSound(null, pos, ModSounds.enchanterForm, SoundCategory.BLOCKS, 0.5F, 0.6F); + PlayerHelper.grantCriterion((ServerPlayerEntity) ctx.getPlayer(), new ResourceLocation(LibMisc.MOD_ID, "main/enchanter_make"), "code_triggered"); + } else { + for(int i = 0; i < 50; i++) { + float red = (float) Math.random(); + float green = (float) Math.random(); + float blue = (float) Math.random(); + + double x = (Math.random() - 0.5) * 6; + double y = (Math.random() - 0.5) * 6; + double z = (Math.random() - 0.5) * 6; + + float velMul = 0.07F; + + float motionx = (float) -x * velMul; + float motiony = (float) -y * velMul; + float motionz = (float) -z * velMul; + WispParticleData data = WispParticleData.wisp((float) Math.random() * 0.15F + 0.15F, red, green, blue); + world.addParticle(data, pos.getX() + 0.5 + x, pos.getY() + 0.5 + y, pos.getZ() + 0.5 + z, motionx, motiony, motionz); + } + } + + return true; + } + return false; + } + + private static boolean tryCompletePistonRelayBinding(ItemUseContext ctx) { + World world = ctx.getWorld(); + BlockPos pos = ctx.getPos(); + PlayerEntity player = ctx.getPlayer(); + + if(((BlockPistonRelay) ModBlocks.pistonRelay).activeBindingAttempts.containsKey(player.getUniqueID())) { + GlobalPos bindPos = ((BlockPistonRelay) ModBlocks.pistonRelay).activeBindingAttempts.get(player.getUniqueID()); + GlobalPos currentPos = GlobalPos.of(world.getDimension().getType(), pos); + + ((BlockPistonRelay) ModBlocks.pistonRelay).activeBindingAttempts.remove(player.getUniqueID()); + ((BlockPistonRelay) ModBlocks.pistonRelay).mappedPositions.put(bindPos, currentPos); + BlockPistonRelay.WorldData.get(world).markDirty(); + + if(bindPos.getDimension() == currentPos.getDimension()) { + PacketHandler.sendToNearby(world, pos, + new PacketBotaniaEffect(PacketBotaniaEffect.EffectType.PARTICLE_BEAM, + bindPos.getPos().getX() + 0.5, bindPos.getPos().getY() + 0.5, bindPos.getPos().getZ() + 0.5, + pos.getX(), pos.getY(), pos.getZ())); + } + + world.playSound(null, player.posX, player.posY, player.posZ, ModSounds.ding, SoundCategory.PLAYERS, 1F, 1F); + return true; + } + return false; + } + @Nonnull @Override public ActionResultType onItemUse(ItemUseContext ctx) { @@ -93,27 +177,14 @@ public class ItemTwigWand extends ItemMod implements ICoordBoundItem { BlockState state = world.getBlockState(pos); Block block = state.getBlock(); Direction side = ctx.getFace(); - - BlockPos boundPos = getBoundTile(stack); - TileEntity boundTile = world.getTileEntity(boundPos); + Optional boundPos = getBindingAttempt(stack); if(player == null) return ActionResultType.PASS; if(player.isSneaking()) { - // Try to complete a binding - if(boundPos.getY() != -1 && !pos.equals(boundPos)) { - if (boundTile instanceof IWandBindable) { - if(((IWandBindable) boundTile).bindTo(player, stack, pos, side)) { - doParticleBeamWithOffset(world, boundPos, pos); - VanillaPacketDispatcher.dispatchTEToNearbyPlayers(world, boundPos); - setBoundTile(stack, UNBOUND_POS); - } - - return ActionResultType.SUCCESS; - } else { - setBoundTile(stack, UNBOUND_POS); - } + if (boundPos.isPresent() && tryCompleteBinding(boundPos.get(), stack, ctx)) { + return ActionResultType.SUCCESS; } if(player.canPlayerEdit(pos, side, stack) @@ -126,40 +197,8 @@ public class ItemTwigWand extends ItemMod implements ICoordBoundItem { } } - if(block == Blocks.LAPIS_BLOCK && ConfigHandler.COMMON.enchanterEnabled.get()) { - Direction.Axis axis = null; - if(TileEnchanter.canEnchanterExist(world, pos, Direction.Axis.X)) - axis = Direction.Axis.X; - else if(TileEnchanter.canEnchanterExist(world, pos, Direction.Axis.Z)) - axis = Direction.Axis.Z; - - if(axis != null) { - if(!world.isRemote) { - world.setBlockState(pos, ModBlocks.enchanter.getDefaultState().with(BotaniaStateProps.ENCHANTER_DIRECTION, axis), 1 | 2); - world.playSound(null, pos, ModSounds.enchanterForm, SoundCategory.BLOCKS, 0.5F, 0.6F); - PlayerHelper.grantCriterion((ServerPlayerEntity) player, new ResourceLocation(LibMisc.MOD_ID, "main/enchanter_make"), "code_triggered"); - } else { - for(int i = 0; i < 50; i++) { - float red = (float) Math.random(); - float green = (float) Math.random(); - float blue = (float) Math.random(); - - double x = (Math.random() - 0.5) * 6; - double y = (Math.random() - 0.5) * 6; - double z = (Math.random() - 0.5) * 6; - - float velMul = 0.07F; - - float motionx = (float) -x * velMul; - float motiony = (float) -y * velMul; - float motionz = (float) -z * velMul; - WispParticleData data = WispParticleData.wisp((float) Math.random() * 0.15F + 0.15F, red, green, blue); - world.addParticle(data, pos.getX() + 0.5 + x, pos.getY() + 0.5 + y, pos.getZ() + 0.5 + z, motionx, motiony, motionz); - } - } - - return ActionResultType.SUCCESS; - } + if(block == Blocks.LAPIS_BLOCK && ConfigHandler.COMMON.enchanterEnabled.get() && tryFormEnchanter(ctx)) { + return ActionResultType.SUCCESS; } if(block instanceof IWandable) { @@ -168,9 +207,9 @@ public class ItemTwigWand extends ItemMod implements ICoordBoundItem { boolean wanded; if(getBindMode(stack) && bindable && player.isSneaking() && ((IWandBindable) tile).canSelect(player, stack, pos, side)) { - if(boundPos.equals(pos)) - setBoundTile(stack, UNBOUND_POS); - else setBoundTile(stack, pos); + if(boundPos.isPresent() && boundPos.get().equals(pos)) + setBindingAttempt(stack, UNBOUND_POS); + else setBindingAttempt(stack, pos); if(world.isRemote) { player.playSound(ModSounds.ding, 0.11F, 1F); @@ -184,19 +223,7 @@ public class ItemTwigWand extends ItemMod implements ICoordBoundItem { return wanded ? ActionResultType.SUCCESS : ActionResultType.FAIL; } - if(!world.isRemote && ((BlockPistonRelay) ModBlocks.pistonRelay).activeBindingAttempts.containsKey(player.getUniqueID())) { - GlobalPos bindPos = ((BlockPistonRelay) ModBlocks.pistonRelay).activeBindingAttempts.get(player.getUniqueID()); - GlobalPos currentPos = GlobalPos.of(world.getDimension().getType(), pos); - - ((BlockPistonRelay) ModBlocks.pistonRelay).activeBindingAttempts.remove(player.getUniqueID()); - ((BlockPistonRelay) ModBlocks.pistonRelay).mappedPositions.put(bindPos, currentPos); - BlockPistonRelay.WorldData.get(world).markDirty(); - - PacketHandler.sendToNearby(world, pos, - new PacketBotaniaEffect(PacketBotaniaEffect.EffectType.PARTICLE_BEAM, - bindPos.getPos().getX() + 0.5, bindPos.getPos().getY() + 0.5, bindPos.getPos().getZ() + 0.5, - pos.getX(), pos.getY(), pos.getZ())); - world.playSound(null, player.posX, player.posY, player.posZ, ModSounds.ding, SoundCategory.PLAYERS, 1F, 1F); + if(!world.isRemote && getBindMode(stack) && tryCompletePistonRelayBinding(ctx)) { return ActionResultType.SUCCESS; } @@ -237,10 +264,11 @@ public class ItemTwigWand extends ItemMod implements ICoordBoundItem { @Override public void inventoryTick(ItemStack par1ItemStack, World world, Entity par3Entity, int par4, boolean par5) { - BlockPos coords = getBoundTile(par1ItemStack); - TileEntity tile = world.getTileEntity(coords); - if(!(tile instanceof IWandBindable)) - setBoundTile(par1ItemStack, UNBOUND_POS); + getBindingAttempt(par1ItemStack).ifPresent(coords -> { + TileEntity tile = world.getTileEntity(coords); + if(!(tile instanceof IWandBindable)) + setBindingAttempt(par1ItemStack, UNBOUND_POS); + }); } @Nonnull @@ -292,17 +320,17 @@ public class ItemTwigWand extends ItemMod implements ICoordBoundItem { return ItemNBTHelper.getInt(stack, TAG_COLOR2, 0); } - public static void setBoundTile(ItemStack stack, BlockPos pos) { + public static void setBindingAttempt(ItemStack stack, BlockPos pos) { ItemNBTHelper.setInt(stack, TAG_BOUND_TILE_X, pos.getX()); ItemNBTHelper.setInt(stack, TAG_BOUND_TILE_Y, pos.getY()); ItemNBTHelper.setInt(stack, TAG_BOUND_TILE_Z, pos.getZ()); } - public static BlockPos getBoundTile(ItemStack stack) { + public static Optional getBindingAttempt(ItemStack stack) { int x = ItemNBTHelper.getInt(stack, TAG_BOUND_TILE_X, 0); int y = ItemNBTHelper.getInt(stack, TAG_BOUND_TILE_Y, -1); int z = ItemNBTHelper.getInt(stack, TAG_BOUND_TILE_Z, 0); - return new BlockPos(x, y, z); + return y < 0 ? Optional.empty() : Optional.of(new BlockPos(x, y, z)); } public static boolean getBindMode(ItemStack stack) { @@ -319,9 +347,9 @@ public class ItemTwigWand extends ItemMod implements ICoordBoundItem { @Override public BlockPos getBinding(ItemStack stack) { - BlockPos bound = getBoundTile(stack); - if(bound.getY() != -1) - return bound; + Optional bound = getBindingAttempt(stack); + if(bound.isPresent()) + return bound.get(); RayTraceResult pos = Minecraft.getInstance().objectMouseOver; if(pos != null && pos.getType() == RayTraceResult.Type.BLOCK) { diff --git a/src/main/resources/assets/botania/lang/en_us.json b/src/main/resources/assets/botania/lang/en_us.json index 83ebca553..bde0e579a 100644 --- a/src/main/resources/assets/botania/lang/en_us.json +++ b/src/main/resources/assets/botania/lang/en_us.json @@ -2930,8 +2930,8 @@ "botania.entry.pistonRelay": "Force Relay", "botania.tagline.pistonRelay": "Remote piston extension", - "botania.page.pistonRelay0": "It seems that infusing $(1)Pistons$(0) with $(4)Mana$(0) results in some interesting adverse effects.$(p)This block tears the fabric of space, relaying any piston force applied to it to the block it's bound to.$(p)To bind it to a block, right click it with a $(1)Wand of the Forest$(0), and right click on the block you want to bind it to.", - "botania.page.pistonRelay1": "To break the block, shift-right click it with a $(1)Wand of the Forest$(0), any other means will destroy it.$(p)Furthermore, this block is highly unstable, and may create uncanny effects, care is to be taken. It also ignores $(1)Sticky Pistons$(0), both in pulling and pushing. This allows for a \"frame\" like setup.", + "botania.page.pistonRelay0": "It seems that infusing $(1)Pistons$(0) with $(4)Mana$(0) results in some interesting adverse effects.$(p)This block tears the fabric of space, relaying any piston force applied to it to the block it's bound to.$(p)To bind it to a block, right click it with a $(1)Wand of the Forest$(0) in $(4)Bind Mode$(0), and right click on the block you want to bind it to.", + "botania.page.pistonRelay1": "Right clicking the block in $(4)Function Mode$(0) shows the existing binding, if any.$(p)To break the block, shift-right click it with a $(1)Wand of the Forest$(0), any other means will destroy it.$(p)Furthermore, this block is highly unstable, and may create uncanny effects, care is to be taken. It also ignores $(1)Sticky Pistons$(0), both in pulling and pushing. This allows for a \"frame\" like setup.", "botania.page.pistonRelay2": "Infusing a $(1)Piston$(0) with $(4)Mana$(0).", "botania.entry.enderHand": "Hand of Ender",