From 5e92da2ab4656cbb23240f69f3e23a9291c9c9c6 Mon Sep 17 00:00:00 2001 From: smcconke <35235905+smcconke@users.noreply.github.com> Date: Sat, 21 Apr 2018 20:21:52 -0400 Subject: [PATCH 1/2] Player can now sneak and place blocks/signs on sign blocks. --- .../block/blocktype/BlockNeedsAttached.java | 8 ++++++++ .../glowstone/block/blocktype/BlockSign.java | 5 +++++ .../glowstone/block/blocktype/BlockType.java | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/src/main/java/net/glowstone/block/blocktype/BlockNeedsAttached.java b/src/main/java/net/glowstone/block/blocktype/BlockNeedsAttached.java index b30e33b890..0f041f0a59 100644 --- a/src/main/java/net/glowstone/block/blocktype/BlockNeedsAttached.java +++ b/src/main/java/net/glowstone/block/blocktype/BlockNeedsAttached.java @@ -30,6 +30,14 @@ public void updatePhysics(GlowBlock me) { } } + /** + * Called to determine if the target block can be attached to + * when right clicking it. + * + * @param block The location the block is being placed at. + * @param against The face the block is being placed against. + * @return Whether the black can be attached to. + */ public boolean canAttachTo(GlowBlock block, BlockFace against) { return !(ItemTable.instance().getBlock( block.getRelative(against.getOppositeFace()).getType()) instanceof BlockNeedsAttached); diff --git a/src/main/java/net/glowstone/block/blocktype/BlockSign.java b/src/main/java/net/glowstone/block/blocktype/BlockSign.java index 8e979d677d..ab2676e15f 100644 --- a/src/main/java/net/glowstone/block/blocktype/BlockSign.java +++ b/src/main/java/net/glowstone/block/blocktype/BlockSign.java @@ -40,4 +40,9 @@ public void afterPlace(GlowPlayer player, GlowBlock block, ItemStack holding, GlowBlockState oldState) { player.openSignEditor(block.getLocation()); } + + @Override + public boolean canPlaceAt(GlowBlock block, BlockFace against) { + return canAttachTo(block, against) || isShiftClickPlace(); + } } diff --git a/src/main/java/net/glowstone/block/blocktype/BlockType.java b/src/main/java/net/glowstone/block/blocktype/BlockType.java index 3b75a731c6..a3b88e3aa8 100644 --- a/src/main/java/net/glowstone/block/blocktype/BlockType.java +++ b/src/main/java/net/glowstone/block/blocktype/BlockType.java @@ -5,6 +5,7 @@ import java.util.Collections; import java.util.List; import lombok.Getter; +import lombok.Setter; import net.glowstone.EventFactory; import net.glowstone.GlowServer; import net.glowstone.block.GlowBlock; @@ -52,6 +53,15 @@ public class BlockType extends ItemType { @Getter protected SoundInfo placeSound = new SoundInfo(Sound.BLOCK_WOOD_BREAK, 1F, 0.75F); + /** + * Determines whether or not the player is sneaking while placing a block. + * + * @return If the player is sneaking while placing. + */ + @Getter + @Setter + private boolean shiftClickPlace = false; + //////////////////////////////////////////////////////////////////////////// // Setters for subclass use @@ -160,6 +170,12 @@ public BlockEntity createBlockEntity(GlowChunk chunk, int cx, int cy, int cz) { * @return Whether the placement is valid. */ public boolean canPlaceAt(GlowBlock block, BlockFace against) { + Material targetMat = ItemTable.instance().getBlock( + block.getRelative(against.getOppositeFace()).getType()).getMaterial(); + + if (targetMat == Material.SIGN_POST || targetMat == Material.WALL_SIGN) { + return isShiftClickPlace(); + } return true; } @@ -316,6 +332,9 @@ public final void rightClickBlock(GlowPlayer player, GlowBlock against, BlockFac ItemStack holding, Vector clickedLoc, EquipmentSlot hand) { GlowBlock target = against.getRelative(face); + // player attempting to place while shifting + setShiftClickPlace(player.isSneaking()); + // prevent building above the height limit if (target.getLocation().getY() >= target.getWorld().getMaxHeight()) { player.sendMessage( From 11f79bcbc63461f079920a9700a2694beeaa86a2 Mon Sep 17 00:00:00 2001 From: smcconke <35235905+smcconke@users.noreply.github.com> Date: Sun, 22 Apr 2018 10:32:36 -0400 Subject: [PATCH 2/2] Checks whether relevant player is sneaking in rightClickBlock. --- .../glowstone/block/blocktype/BlockSign.java | 6 +++- .../glowstone/block/blocktype/BlockType.java | 35 ++++++++----------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/glowstone/block/blocktype/BlockSign.java b/src/main/java/net/glowstone/block/blocktype/BlockSign.java index ab2676e15f..f25b972446 100644 --- a/src/main/java/net/glowstone/block/blocktype/BlockSign.java +++ b/src/main/java/net/glowstone/block/blocktype/BlockSign.java @@ -2,6 +2,7 @@ import net.glowstone.block.GlowBlock; import net.glowstone.block.GlowBlockState; +import net.glowstone.block.ItemTable; import net.glowstone.block.entity.BlockEntity; import net.glowstone.block.entity.SignEntity; import net.glowstone.chunk.GlowChunk; @@ -43,6 +44,9 @@ public void afterPlace(GlowPlayer player, GlowBlock block, ItemStack holding, @Override public boolean canPlaceAt(GlowBlock block, BlockFace against) { - return canAttachTo(block, against) || isShiftClickPlace(); + Material targetMat = ItemTable.instance().getBlock( + block.getRelative(against.getOppositeFace()).getType()).getMaterial(); + return canAttachTo(block, against) || targetMat == Material.SIGN_POST + || targetMat == Material.WALL_SIGN; } } diff --git a/src/main/java/net/glowstone/block/blocktype/BlockType.java b/src/main/java/net/glowstone/block/blocktype/BlockType.java index a3b88e3aa8..ab51256634 100644 --- a/src/main/java/net/glowstone/block/blocktype/BlockType.java +++ b/src/main/java/net/glowstone/block/blocktype/BlockType.java @@ -5,7 +5,6 @@ import java.util.Collections; import java.util.List; import lombok.Getter; -import lombok.Setter; import net.glowstone.EventFactory; import net.glowstone.GlowServer; import net.glowstone.block.GlowBlock; @@ -53,15 +52,6 @@ public class BlockType extends ItemType { @Getter protected SoundInfo placeSound = new SoundInfo(Sound.BLOCK_WOOD_BREAK, 1F, 0.75F); - /** - * Determines whether or not the player is sneaking while placing a block. - * - * @return If the player is sneaking while placing. - */ - @Getter - @Setter - private boolean shiftClickPlace = false; - //////////////////////////////////////////////////////////////////////////// // Setters for subclass use @@ -170,12 +160,6 @@ public BlockEntity createBlockEntity(GlowChunk chunk, int cx, int cy, int cz) { * @return Whether the placement is valid. */ public boolean canPlaceAt(GlowBlock block, BlockFace against) { - Material targetMat = ItemTable.instance().getBlock( - block.getRelative(against.getOppositeFace()).getType()).getMaterial(); - - if (targetMat == Material.SIGN_POST || targetMat == Material.WALL_SIGN) { - return isShiftClickPlace(); - } return true; } @@ -331,9 +315,8 @@ public void updatePhysics(GlowBlock block) { public final void rightClickBlock(GlowPlayer player, GlowBlock against, BlockFace face, ItemStack holding, Vector clickedLoc, EquipmentSlot hand) { GlowBlock target = against.getRelative(face); - - // player attempting to place while shifting - setShiftClickPlace(player.isSneaking()); + final Material targetMat = ItemTable.instance().getBlock( + target.getRelative(face.getOppositeFace()).getType()).getMaterial(); // prevent building above the height limit if (target.getLocation().getY() >= target.getWorld().getMaxHeight()) { @@ -369,7 +352,19 @@ public final void rightClickBlock(GlowPlayer player, GlowBlock against, BlockFac } // call canBuild event - boolean canBuild = canPlaceAt(target, face); + boolean canBuild = true; + switch (targetMat) { + case SIGN_POST: + case WALL_SIGN: + if (player.isSneaking()) { + canBuild = canPlaceAt(target, face); + } else { + return; + } + break; + default: + canBuild = canPlaceAt(target, face); + } BlockCanBuildEvent canBuildEvent = new BlockCanBuildEvent(target, getId(), canBuild); if (!EventFactory.getInstance().callEvent(canBuildEvent).isBuildable()) { //revert(player, target);