Skip to content

Commit

Permalink
Add experimental support for the back side of signs
Browse files Browse the repository at this point in the history
This is a fairly large rewrite, and due to that will not make it into CraftBook 3. There is also a possibility that there might be some bugs that need sorting out.
  • Loading branch information
me4502 committed Jul 9, 2023
1 parent a2740ca commit 39848d2
Show file tree
Hide file tree
Showing 121 changed files with 1,149 additions and 738 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,61 @@
import com.google.common.base.Preconditions;
import com.sk89q.worldedit.entity.Player;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.block.sign.Side;
import org.bukkit.block.sign.SignSide;
import org.enginehub.craftbook.mechanics.variables.VariableKey;
import org.enginehub.craftbook.mechanics.variables.VariableManager;
import org.enginehub.craftbook.util.ParsingUtil;

import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* A ChangedSign represents a single side of a sign block.
*/
public class ChangedSign {

private final Block block;
private Sign sign;
private String[] lines;
private String[] oldLines;

public ChangedSign(Block block, String[] lines, CraftBookPlayer player) {
this(block, lines);
private final Side side;

if (player != null) {
checkPlayerVariablePermissions(player);
}
}

public ChangedSign(Block block, String[] lines) {
Preconditions.checkNotNull(block, "block");
private Sign sign;
private Component[] lines;
private Component[] oldLines;

private ChangedSign(Block block, Side side, Component[] lines, CraftBookPlayer player) {
this.block = block;
this.side = side;

if (lines == null) {
this.flushLines();
} else {
this.lines = lines;
this.oldLines = new String[this.lines.length];
this.oldLines = new Component[this.lines.length];
System.arraycopy(this.lines, 0, this.oldLines, 0, this.lines.length);
}

if (player != null) {
checkPlayerVariablePermissions(player);
}
}

public void checkPlayerVariablePermissions(CraftBookPlayer player) {
if (this.lines != null && VariableManager.instance != null) {
for (int i = 0; i < 4; i++) {
String line = this.lines[i];
Component line = this.lines[i];
for (VariableKey variableKey : VariableManager.getPossibleVariables(line, player)) {
if (!variableKey.hasPermission(player, "use")) {
setLine(i, line.replace('%' + variableKey.getOriginalForm() + '%', ""));
TextReplacementConfig config = TextReplacementConfig.builder()
.matchLiteral("%" + variableKey.getOriginalForm() + "%")
.replacement("").build();
setLine(i, line.replaceText(config));
}
}
}
Expand All @@ -82,8 +90,15 @@ public Sign getSign() {
return sign;
}

public Material getType() {
public SignSide getSignSide() {
return getSign().getSide(this.side);
}

public Side getSide() {
return this.side;
}

public Material getType() {
return block.getType();
}

Expand All @@ -99,23 +114,23 @@ public int getZ() {
return this.block.getZ();
}

public String[] getLines() {
public Component[] getLines() {
return this.lines;
}

public String getLine(int index) throws IndexOutOfBoundsException {
public Component getLine(int index) throws IndexOutOfBoundsException {
return this.getLine(index, null);
}

public String getLine(int index, @Nullable Player player) throws IndexOutOfBoundsException {
public Component getLine(int index, @Nullable Player player) throws IndexOutOfBoundsException {
return ParsingUtil.parseLine(this.lines[index], player);
}

public String getRawLine(int index) throws IndexOutOfBoundsException {
public Component getRawLine(int index) throws IndexOutOfBoundsException {
return this.lines[index];
}

public void setLine(int index, String line) throws IndexOutOfBoundsException {
public void setLine(int index, Component line) throws IndexOutOfBoundsException {
this.lines[index] = line;
}

Expand All @@ -129,18 +144,18 @@ public boolean update(boolean force) {
}

for (int i = 0; i < 4; i++) {
getSign().line(i, Component.text(lines[i]));
getSignSide().line(i, lines[i]);
}
System.arraycopy(this.lines, 0, this.oldLines, 0, this.lines.length);

return getSign().update(force, false);
}

public void setLines(String[] lines) {
public void setLines(Component[] lines) {
this.lines = lines;
}

public void setOldLines(String[] oldLines) {
public void setOldLines(Component[] oldLines) {
this.oldLines = oldLines;
}

Expand All @@ -156,10 +171,10 @@ public boolean hasChanged() {

public void flushLines() {
this.sign = null;
this.lines = this.getSign().getLines();
this.lines = this.getSignSide().lines().toArray(new Component[lines.length]);

if (this.oldLines == null) {
this.oldLines = new String[lines.length];
this.oldLines = new Component[lines.length];
}

System.arraycopy(this.lines, 0, this.oldLines, 0, this.lines.length);
Expand All @@ -178,6 +193,7 @@ public boolean updateSign(ChangedSign sign) {
public boolean equals(Object o) {
if (o instanceof ChangedSign other) {
return Objects.equals(other.getType(), getType())
&& other.getSide() == getSide()
&& other.getX() == getX()
&& other.getY() == getY()
&& other.getZ() == getZ()
Expand All @@ -190,11 +206,30 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(getType(), Arrays.hashCode(this.lines), getX(), getY(), getZ(), block.getWorld().getUID());
return Objects.hash(getType(), side, Arrays.hashCode(this.lines), getX(), getY(), getZ(), block.getWorld().getUID());
}

@Override
public String toString() {
return lines[0] + '|' + lines[1] + '|' + lines[2] + '|' + lines[3];
return Arrays.stream(this.lines).map(PlainTextComponentSerializer.plainText()::serialize).collect(Collectors.joining("|"));
}

public static ChangedSign create(@Nonnull Sign sign, @Nonnull Side side) {
return create(sign.getBlock(), side, sign.getSide(side).lines().toArray(new Component[0]), null);
}

public static ChangedSign create(@Nonnull Sign sign, @Nonnull Side side, @Nullable CraftBookPlayer player) {
return create(sign.getBlock(), side, sign.getSide(side).lines().toArray(new Component[0]), player);
}

public static ChangedSign create(@Nonnull Block block, @Nonnull Side side) {
return create(block, side, null, null);
}

public static ChangedSign create(@Nonnull Block block, @Nonnull Side side, @Nullable Component[] lines, @Nullable CraftBookPlayer player) {
Preconditions.checkNotNull(block, "block");
Preconditions.checkNotNull(side, "side");

return new ChangedSign(block, side, lines, player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,15 @@
package org.enginehub.craftbook.bukkit.util;

import com.sk89q.worldedit.math.BlockVector3;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign;
import org.enginehub.craftbook.ChangedSign;
import org.enginehub.craftbook.CraftBookPlayer;
import org.enginehub.craftbook.util.SignUtil;

public final class CraftBookBukkitUtil {

private CraftBookBukkitUtil() {
}

public static ChangedSign toChangedSign(Block sign) {
return toChangedSign(sign, null, null);
}

public static ChangedSign toChangedSign(Block block, String[] lines, CraftBookPlayer player) {
if (!SignUtil.isSign(block)) {
return null;
}
return new ChangedSign(block, lines, player);
}

public static Sign toSign(ChangedSign sign) {
try {
if (sign.hasChanged()) sign.update(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockTypes;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.sign.Side;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.SignChangeEvent;
Expand All @@ -33,7 +35,6 @@
import org.enginehub.craftbook.CraftBook;
import org.enginehub.craftbook.CraftBookPlayer;
import org.enginehub.craftbook.bukkit.CraftBookPlugin;
import org.enginehub.craftbook.bukkit.util.CraftBookBukkitUtil;
import org.enginehub.craftbook.util.BlockParser;
import org.enginehub.craftbook.util.EventUtil;
import org.enginehub.craftbook.util.RegexUtil;
Expand Down Expand Up @@ -107,45 +108,49 @@ public void onPlayerMove(final PlayerMoveEvent event) {
Block sign = block.getRelative(BlockFace.DOWN);

if (SignUtil.isSign(sign)) {
final ChangedSign s = CraftBookBukkitUtil.toChangedSign(sign);
for (Side side : Side.values()) {
final ChangedSign s = ChangedSign.create(sign, side);

if (s.getLine(1).equals("[Jump]")) {
String signLine1 = PlainTextComponentSerializer.plainText().serialize(s.getLine(1));
if (signLine1.equals("[Jump]")) {
String signLine2 = PlainTextComponentSerializer.plainText().serialize(s.getLine(2, CraftBookPlugin.inst().wrapPlayer(event.getPlayer())));

CraftBookPlugin.logDebugMessage("Jump sign found where player jumped!", "bounce-blocks");
CraftBookPlugin.logDebugMessage("Jump sign found where player jumped!", "bounce-blocks");

double x = 0, y, z = 0;
boolean straight = s.getLine(2).startsWith("!");
double x = 0, y, z = 0;
boolean straight = signLine2.startsWith("!");

String[] bits = RegexUtil.COMMA_PATTERN.split(s.getLine(2).replace("!", ""));
if (bits.length == 0) {
y = 0.5;
} else if (bits.length == 1) {
try {
y = Double.parseDouble(bits[0]);
} catch (NumberFormatException e) {
String[] bits = RegexUtil.COMMA_PATTERN.split(signLine2.replace("!", ""));
if (bits.length == 0) {
y = 0.5;
} else if (bits.length == 1) {
try {
y = Double.parseDouble(bits[0]);
} catch (NumberFormatException e) {
y = 0.5;
}
} else {
x = Double.parseDouble(bits[0]);
y = Double.parseDouble(bits[1]);
z = Double.parseDouble(bits[2]);
}
} else {
x = Double.parseDouble(bits[0]);
y = Double.parseDouble(bits[1]);
z = Double.parseDouble(bits[2]);
}

if (!straight) {
if (!straight) {

Vector facing = event.getTo().getDirection();
Vector facing = event.getTo().getDirection();

//Find out the angle they are facing. This is completely to do with horizontals. No verticals are taken into account.
double angle = Math.atan2(facing.getX(), facing.getZ());
//Find out the angle they are facing. This is completely to do with horizontals. No verticals are taken into account.
double angle = Math.atan2(facing.getX(), facing.getZ());

x = Math.sin(angle) * x;
z = Math.cos(angle) * z;
}
x = Math.sin(angle) * x;
z = Math.cos(angle) * z;
}

event.getPlayer().setVelocity(new Vector(x, y, z));
event.getPlayer().setFallDistance(-20f);
event.getPlayer().setVelocity(new Vector(x, y, z));
event.getPlayer().setFallDistance(-20f);
}
return;
}
return;
}
}

Expand Down Expand Up @@ -175,7 +180,6 @@ public void onPlayerMove(final PlayerMoveEvent event) {

@EventHandler(priority = EventPriority.HIGH)
public void onSignChange(SignChangeEvent event) {

if (!EventUtil.passesFilter(event)) return;

if (!event.getLine(1).equalsIgnoreCase("[jump]")) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.enginehub.craftbook.CraftBook;
import org.enginehub.craftbook.CraftBookPlayer;
import org.enginehub.craftbook.bukkit.CraftBookPlugin;
import org.enginehub.craftbook.bukkit.util.CraftBookBukkitUtil;
import org.enginehub.craftbook.util.EventUtil;
import org.enginehub.craftbook.util.SignUtil;
import org.enginehub.craftbook.util.events.SourcedBlockRedstoneEvent;
Expand Down Expand Up @@ -99,13 +98,15 @@ public void onBlockRedstoneChange(SourcedBlockRedstoneEvent event) {

Block block = event.getBlock();
if (SignUtil.isSign(block)) {
ChangedSign sign = CraftBookBukkitUtil.toChangedSign(block);
Sign bukktiSign = (Sign) block.getState(false);
ChangedSign sign = ChangedSign.create(block, bukktiSign.getInteractableSideFor(event.getSource().getLocation()));

if (!sign.getLine(1).equals("[Chunk]")) {
String line1 = PlainTextComponentSerializer.plainText().serialize(sign.getLine(1));
if (!line1.equals("[Chunk]")) {
return;
}

sign.setLine(3, event.isOn() ? "" : "OFF");
sign.setLine(3, Component.text(event.isOn() ? "" : "OFF"));
sign.update(false);

updateChunkTicket(event.getBlock().getChunk());
Expand All @@ -119,16 +120,19 @@ public void onBlockBreak(BlockBreakEvent event) {
}

if (SignUtil.isSign(event.getBlock())) {
ChangedSign sign = CraftBookBukkitUtil.toChangedSign(event.getBlock());
Sign sign = (Sign) event.getBlock().getState(false);

if (!sign.getLine(1).equals("[Chunk]")) {
return;
}
for (Side side : Side.values()) {
if (!sign.getSide(side).getLine(1).equals("[Chunk]")) {
continue;
}

Bukkit.getScheduler().runTask(
CraftBookPlugin.inst(),
() -> updateChunkTicket(event.getBlock().getChunk())
);
Bukkit.getRegionScheduler().execute(
CraftBookPlugin.inst(),
event.getBlock().getLocation(),
() -> updateChunkTicket(event.getBlock().getChunk())
);
}
}
}

Expand Down

0 comments on commit 39848d2

Please sign in to comment.