Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Item Placement with Signs #902

Merged
merged 3 commits into from Apr 25, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/glowstone/block/blocktype/BlockSign.java
Expand Up @@ -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();
}
}
19 changes: 19 additions & 0 deletions src/main/java/net/glowstone/block/blocktype/BlockType.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work properly in the case where multiple players try to interact with the same block in a short timespan. Instead, whatever method calls canPlaceAt or canAttachTo should check whether the relevant player isSneaking().


////////////////////////////////////////////////////////////////////////////
// Setters for subclass use

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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(
Expand Down