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

[Feature / Cleanup] Sign rework (WIP) #679

Open
wants to merge 123 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
123 commits
Select commit Hold shift + click to select a range
ed29a0a
begin new sign API
DerToaster98 Aug 1, 2024
0825279
add base class for signs and registration
DerToaster98 Aug 1, 2024
b66b5e0
add craft sign => For stuff like cruise signs or remote signs
DerToaster98 Aug 1, 2024
6a3ad90
implement listener that calls the individual registered signs
DerToaster98 Aug 1, 2024
9ca4816
add base for cruise-like signs
DerToaster98 Aug 2, 2024
c80df99
add overloaded internalProcessing method for craftsigns
DerToaster98 Aug 2, 2024
07e829e
add method to get ident of sign instance
DerToaster98 Aug 2, 2024
edbd6ef
call detect event with the sign
DerToaster98 Aug 2, 2024
0b3f467
migrate name sign to new sign API
DerToaster98 Aug 2, 2024
95cbe5b
organize imports
DerToaster98 Aug 2, 2024
f82bb7b
reimplement release sign with new sign api
DerToaster98 Aug 2, 2024
4692881
reimplement scuttle sign with new sign API
DerToaster98 Aug 2, 2024
08611a9
register release and scuttle signs
DerToaster98 Aug 2, 2024
d4f9cbb
migrate Move and RMove signs
DerToaster98 Aug 2, 2024
8f92799
move perm check to canPlayerUseOn
DerToaster98 Aug 4, 2024
81cf5a9
migrate helm sign
DerToaster98 Aug 4, 2024
99aa146
rename method to "onCraftisBusy" is it isn't always a parent craft bu…
DerToaster98 Aug 4, 2024
a66a0af
allow double colons in idents
DerToaster98 Aug 4, 2024
c4623f5
add comment for later
DerToaster98 Aug 4, 2024
9575095
make sign listener abstract and add abstraction layer to signs
DerToaster98 Aug 10, 2024
2997b1e
initialize sign listener like the version specific stuff
DerToaster98 Aug 10, 2024
604abeb
refactor sign base classes to use the abstraction class
DerToaster98 Aug 10, 2024
c1bf27d
add base for info signs
DerToaster98 Aug 10, 2024
0edd8cb
refactors
DerToaster98 Aug 10, 2024
d679689
add 1.21 implementation of signlistener
DerToaster98 Aug 10, 2024
02fe15e
fix imports
DerToaster98 Aug 10, 2024
ef9ca4b
add 1.20 implementation of listener
DerToaster98 Aug 10, 2024
383683e
add 1.18 signListener implementation
DerToaster98 Aug 10, 2024
f8e6c02
migrate teleport sign and fix move sign
DerToaster98 Aug 10, 2024
7260f9d
fix helm sign
DerToaster98 Aug 10, 2024
7b77377
add comment in TeleportSign
DerToaster98 Aug 10, 2024
ddec33c
migrate PilotSign
DerToaster98 Aug 10, 2024
a3b2c52
migrate cruise sign
DerToaster98 Aug 10, 2024
6913afe
forgot that
DerToaster98 Aug 10, 2024
57c9d98
migrate cruise signs
DerToaster98 Aug 10, 2024
38c644a
fix namesign
DerToaster98 Aug 10, 2024
0d61dd5
migrate speed sign
DerToaster98 Aug 10, 2024
f9681d4
own wrapper for SignTranslateEvent
DerToaster98 Aug 10, 2024
50b83dc
actually return something
DerToaster98 Aug 10, 2024
d95c1d0
migrate speed sign
DerToaster98 Aug 10, 2024
2a53a7c
forgot those
DerToaster98 Aug 10, 2024
35ea966
move status sign class
DerToaster98 Aug 11, 2024
d882603
add constants for colors and add enum for update reason
DerToaster98 Aug 11, 2024
1ede8b7
migrate status sign
DerToaster98 Aug 11, 2024
b546e7a
fix speed sign
DerToaster98 Aug 11, 2024
1bcae23
migrate craft pilot sign
DerToaster98 Aug 14, 2024
e89dbf5
register calls
DerToaster98 Aug 14, 2024
01cf53c
remove old sign
DerToaster98 Aug 14, 2024
540d89f
add helper methods
DerToaster98 Aug 14, 2024
47638bd
add todo notes
DerToaster98 Aug 15, 2024
bea521b
move status sign back to where it was
DerToaster98 Aug 19, 2024
e9b7c62
subscribe to event to re-register the craft sigsn
DerToaster98 Aug 19, 2024
5efcc73
remove unnecessary if
DerToaster98 Aug 19, 2024
4537421
Optional => Nullable string
DerToaster98 Aug 19, 2024
69d701d
typo fix
DerToaster98 Aug 19, 2024
b307c87
add overloaded register method
DerToaster98 Aug 19, 2024
8219dc3
Optional<Craft> => Nullable Craft
DerToaster98 Aug 19, 2024
0b3d5d1
remove static modifier
DerToaster98 Aug 19, 2024
344afce
refactor equals function
DerToaster98 Aug 19, 2024
a133c7b
documentation
DerToaster98 Aug 20, 2024
3a261a6
small corrections
DerToaster98 Aug 20, 2024
a755453
add base for subcraft signs
DerToaster98 Aug 20, 2024
e78f2fd
migrate Remote Sign
DerToaster98 Aug 27, 2024
ebc5a3b
Migrate subcraft rotate sign
DerToaster98 Aug 27, 2024
e493b78
fix refactor artifact
DerToaster98 Aug 27, 2024
dc0075c
remove unneeded variable => use the field!
DerToaster98 Aug 27, 2024
f8414ed
migrate contacts sign
DerToaster98 Aug 28, 2024
36fbe17
fix error
DerToaster98 Aug 28, 2024
1b512f2
add isEmpty() method and add implementation for equals
DerToaster98 Aug 28, 2024
c84a81a
if the other is null it will be false. Always
DerToaster98 Aug 28, 2024
48add81
begin migrating SignTranslateEvent to use the signwrapper (issue #688)
DerToaster98 Aug 28, 2024
104be46
add methods to directly allow Component as header dient
DerToaster98 Aug 28, 2024
22f1520
use new methods
DerToaster98 Aug 28, 2024
c583ab4
todo notes
DerToaster98 Aug 28, 2024
dce6e0f
use new methods in SignTranslateEvent
DerToaster98 Aug 28, 2024
514a447
add facing value to event
DerToaster98 Aug 29, 2024
fb0446e
add more utility and itnerface methods
DerToaster98 Aug 29, 2024
376986c
call the adatper
DerToaster98 Aug 29, 2024
4e407a3
implement methods in adatpers
DerToaster98 Aug 29, 2024
9531e36
adjust information signs
DerToaster98 Aug 29, 2024
630dd47
deprecation note
DerToaster98 Aug 29, 2024
13831c2
Merge branch 'main' into sign-rework
DerToaster98 Aug 29, 2024
9b20ae0
fix method name
DerToaster98 Aug 29, 2024
68c62a1
fix compile errors
DerToaster98 Aug 29, 2024
92141d4
fix release sign => it is not dependant on a craft existing!
DerToaster98 Aug 29, 2024
2df78c5
fix startup error => can't retrieve ident while registering
DerToaster98 Sep 5, 2024
960b30a
fix getSignWrappers() method for sign => NPE
DerToaster98 Sep 5, 2024
060d8b4
fix status sign
DerToaster98 Sep 5, 2024
debb191
craft is nullable
DerToaster98 Sep 5, 2024
39e08a5
corrections to BlockFace access
DerToaster98 Sep 5, 2024
9b7ea60
more fixes regarding getting the blockface of a sign...
DerToaster98 Sep 5, 2024
48a9bc1
NPE prevention
DerToaster98 Sep 5, 2024
ca35cd3
add TODO note
DerToaster98 Sep 5, 2024
464fcd6
remove spaces from suffix
DerToaster98 Sep 5, 2024
b65562e
correction => if the ident has trailing : => don't add them yourself …
DerToaster98 Sep 5, 2024
132f179
startup correction
DerToaster98 Sep 5, 2024
5ff7af8
forgot the space
DerToaster98 Sep 5, 2024
9fb1917
avoid double inversion
DerToaster98 Sep 5, 2024
e09c26d
crafttype is specified in second, not the third line
DerToaster98 Sep 5, 2024
6b1d67f
typo
DerToaster98 Sep 5, 2024
eb50ec4
remote sign correction => don't use old list, use HashMap and use com…
DerToaster98 Sep 5, 2024
8e76762
fix information signs not updating
DerToaster98 Sep 6, 2024
d86b510
comment out debug logs
DerToaster98 Sep 6, 2024
5ac3fc6
call update on the state, not on the block...
DerToaster98 Sep 6, 2024
7bf7297
more elegant solution
DerToaster98 Sep 6, 2024
0178865
actually perform the update, event uses the signwrapper under the hoo…
DerToaster98 Sep 6, 2024
9da541b
fix status sign (reimplement based on @Vaan1310 changes)
DerToaster98 Sep 6, 2024
26d1497
NPE prevention
DerToaster98 Sep 6, 2024
1153498
remove unnecessary toUpperCase()
DerToaster98 Sep 6, 2024
731aae7
add eventtype enum (to api classes)
DerToaster98 Sep 6, 2024
baad4f8
refactor for eventtype enum (implementations)
DerToaster98 Sep 6, 2024
cdd55d1
refactor for eventtype enum (implementations (simple))
DerToaster98 Sep 6, 2024
f4eef4a
Status Sign: round to 2 decimals
DerToaster98 Sep 6, 2024
4bc2ad4
supply eventType and use nullable for sign get access
DerToaster98 Sep 10, 2024
21c61c0
just call it override
DerToaster98 Sep 10, 2024
9acc31a
add registry base class, implement it, and use it
DerToaster98 Sep 10, 2024
aef31b4
adjust comment
DerToaster98 Sep 10, 2024
970d49d
adjust comment
DerToaster98 Sep 10, 2024
0da888d
remove old code
DerToaster98 Sep 10, 2024
bf29040
index out of bounds exception patch
DerToaster98 Sep 21, 2024
d44c29d
add getter for crafttype
DerToaster98 Sep 29, 2024
ca320fc
cherry pick commit
DerToaster98 Oct 5, 2024
dcddc05
cherry pick commit
DerToaster98 Oct 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 49 additions & 17 deletions Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class Movecraft extends JavaPlugin {
private WorldHandler worldHandler;
private SmoothTeleport smoothTeleport;
private AsyncManager asyncManager;
private AbstractSignListener abstractSignListener;
private WreckManager wreckManager;

public static synchronized Movecraft getInstance() {
Expand Down Expand Up @@ -132,6 +133,12 @@ public void onEnable() {
getLogger().warning("Falling back to bukkit teleportation provider.");
}
}

final Class<?> signListenerClass = Class.forName("net.countercraft.movecraft.compat." + WorldHandler.getPackageName(minecraftVersion) + ".SignListener");
if (AbstractSignListener.class.isAssignableFrom(signListenerClass)) {
abstractSignListener = (AbstractSignListener) signListenerClass.getConstructor().newInstance();
getServer().getPluginManager().registerEvents(abstractSignListener, this);
}
}
catch (final Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -209,37 +216,60 @@ public void onEnable() {
getCommand("crafttype").setExecutor(new CraftTypeCommand());
getCommand("craftinfo").setExecutor(new CraftInfoCommand());

// Naming scheme: If it has parameters, append a double colon except if it is a subcraft
// Parameters follow on the following lines
getServer().getPluginManager().registerEvents(new BlockListener(), this);
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
getServer().getPluginManager().registerEvents(new ChunkManager(), this);
getServer().getPluginManager().registerEvents(new AscendSign(), this);
getServer().getPluginManager().registerEvents(new CraftSign(), this);
getServer().getPluginManager().registerEvents(new CruiseSign(), this);
getServer().getPluginManager().registerEvents(new DescendSign(), this);
getServer().getPluginManager().registerEvents(new HelmSign(), this);
getServer().getPluginManager().registerEvents(new MoveSign(), this);
getServer().getPluginManager().registerEvents(new NameSign(), this);
getServer().getPluginManager().registerEvents(new PilotSign(), this);
getServer().getPluginManager().registerEvents(new RelativeMoveSign(), this);
getServer().getPluginManager().registerEvents(new ReleaseSign(), this);
getServer().getPluginManager().registerEvents(new RemoteSign(), this);
getServer().getPluginManager().registerEvents(new SpeedSign(), this);
getServer().getPluginManager().registerEvents(new SubcraftRotateSign(), this);
getServer().getPluginManager().registerEvents(new TeleportSign(), this);
getServer().getPluginManager().registerEvents(new ScuttleSign(), this);
//getServer().getPluginManager().registerEvents(new AscendSign(), this);
AbstractMovecraftSign.register("Ascend:", new AscendSign("Ascend:"));
//getServer().getPluginManager().registerEvents(new CruiseSign(), this);
AbstractMovecraftSign.register("Cruise:", new CruiseSign("Cruise:"));
//getServer().getPluginManager().registerEvents(new DescendSign(), this);
AbstractMovecraftSign.register("Descend:", new DescendSign("Descend:"));
//getServer().getPluginManager().registerEvents(new HelmSign(), this);
AbstractMovecraftSign.register("[Helm]", new HelmSign());
AbstractMovecraftSign.register(HelmSign.PRETTY_HEADER, new HelmSign());
//getServer().getPluginManager().registerEvents(new MoveSign(), this);
AbstractMovecraftSign.register("Move:", new MoveSign());
//getServer().getPluginManager().registerEvents(new NameSign(), this);
AbstractMovecraftSign.register("Name:", new NameSign());
//getServer().getPluginManager().registerEvents(new PilotSign(), this);
AbstractMovecraftSign.register("Pilot:", new PilotSign());
//getServer().getPluginManager().registerEvents(new RelativeMoveSign(), this);
AbstractMovecraftSign.register("RMove:", new RelativeMoveSign());
//getServer().getPluginManager().registerEvents(new ReleaseSign(), this);
AbstractMovecraftSign.register("Release", new ReleaseSign());
//getServer().getPluginManager().registerEvents(new RemoteSign(), this);
AbstractMovecraftSign.register("Remote Sign", new RemoteSign());
//getServer().getPluginManager().registerEvents(new SpeedSign(), this);
AbstractMovecraftSign.register("Speed:", new SpeedSign());
AbstractMovecraftSign.register("Status:", new StatusSign());
AbstractMovecraftSign.register("Contacts:", new ContactsSign());
//getServer().getPluginManager().registerEvents(new SubcraftRotateSign(), this);
AbstractMovecraftSign.register("Subcraft Rotate", new SubcraftRotateSign(CraftManager.getInstance()::getCraftTypeFromString, Movecraft::getInstance));
//getServer().getPluginManager().registerEvents(new TeleportSign(), this);
AbstractMovecraftSign.register("Teleport:", new TeleportSign());
//getServer().getPluginManager().registerEvents(new ScuttleSign(), this);
AbstractMovecraftSign.register("Scuttle", new ScuttleSign());
getServer().getPluginManager().registerEvents(new CraftPilotListener(), this);
getServer().getPluginManager().registerEvents(new CraftReleaseListener(), this);
// Moved to compat section!
//getServer().getPluginManager().registerEvents(new SignListener(), this);

AbstractMovecraftSign.registerCraftPilotSigns(CraftManager.getInstance().getCraftTypes(), CraftPilotSign::new);

var contactsManager = new ContactsManager();
contactsManager.runTaskTimerAsynchronously(this, 0, 20);
getServer().getPluginManager().registerEvents(contactsManager, this);
getServer().getPluginManager().registerEvents(new ContactsSign(), this);
//getServer().getPluginManager().registerEvents(new ContactsSign(), this);
getServer().getPluginManager().registerEvents(new CraftTypeListener(), this);
getCommand("contacts").setExecutor(new ContactsCommand());

var statusManager = new StatusManager();
statusManager.runTaskTimerAsynchronously(this, 0, 1);
getServer().getPluginManager().registerEvents(statusManager, this);
getServer().getPluginManager().registerEvents(new StatusSign(), this);
//getServer().getPluginManager().registerEvents(new StatusSign(), this);

logger.info("[V " + getDescription().getVersion() + "] has been enabled.");
}
Expand Down Expand Up @@ -337,6 +367,8 @@ public AsyncManager getAsyncManager() {
return asyncManager;
}

public AbstractSignListener getAbstractSignListener() {return abstractSignListener;}

public @NotNull WreckManager getWreckManager(){
return wreckManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import net.countercraft.movecraft.Movecraft;
import net.countercraft.movecraft.craft.CraftManager;
import net.countercraft.movecraft.localisation.I18nSupport;
import net.countercraft.movecraft.sign.AbstractMovecraftSign;
import net.countercraft.movecraft.sign.CraftPilotSign;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,107 +3,100 @@
import net.countercraft.movecraft.MovecraftLocation;
import net.countercraft.movecraft.craft.Craft;
import net.countercraft.movecraft.craft.type.CraftType;
import net.countercraft.movecraft.events.CraftDetectEvent;
import net.countercraft.movecraft.events.SignTranslateEvent;
import org.bukkit.ChatColor;
import org.bukkit.Tag;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import net.countercraft.movecraft.sign.AbstractInformationSign;
import net.countercraft.movecraft.sign.AbstractSignListener;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.Style;
import org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class ContactsSign implements Listener {
private static final String HEADER = "Contacts:";
import java.util.List;

@EventHandler
public void onCraftDetect(@NotNull CraftDetectEvent event) {
World world = event.getCraft().getWorld();
for (MovecraftLocation location : event.getCraft().getHitBox()) {
var block = location.toBukkit(world).getBlock();
if (!Tag.SIGNS.isTagged(block.getType()))
continue;
public class ContactsSign extends AbstractInformationSign {

BlockState state = block.getState();
if (!(state instanceof Sign sign))
continue;
protected final int MAX_DISTANCE_COLOR_RED = 64 * 64;
protected final int MAX_DISTANCE_COLOR_YELLOW = 128 * 128;

if (!ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase(HEADER))
continue;

sign.setLine(1, "");
sign.setLine(2, "");
sign.setLine(3, "");
sign.update();
}
}

@EventHandler
public final void onSignTranslateEvent(@NotNull SignTranslateEvent event) {
if (!ChatColor.stripColor(event.getLine(0)).equalsIgnoreCase(HEADER))
return;
protected @NotNull Component contactsLine(@NotNull Craft base, @NotNull Craft target) {
MovecraftLocation baseCenter = base.getHitBox().getMidPoint();
MovecraftLocation targetCenter = target.getHitBox().getMidPoint();
int distanceSquared = baseCenter.distanceSquared(targetCenter);

Craft base = event.getCraft();
int line = 1;
for (Craft target : base.getDataTag(Craft.CONTACTS)) {
if (line > 3)
break;
String craftTypeName = target.getType().getStringProperty(CraftType.NAME);
if (craftTypeName.length() > 9)
craftTypeName = craftTypeName.substring(0, 7);

event.setLine(line++, contactsLine(base, target));
Style style = STYLE_COLOR_GREEN;
if (distanceSquared <= MAX_DISTANCE_COLOR_RED) {
style = STYLE_COLOR_RED;
}
while (line <= 3) {
event.setLine(line++, "");
else if (distanceSquared <= MAX_DISTANCE_COLOR_YELLOW) {
style = STYLE_COLOR_YELLOW;
}
}

private static @NotNull String contactsLine(@NotNull Craft base, @NotNull Craft target) {
MovecraftLocation baseCenter = base.getHitBox().getMidPoint();
MovecraftLocation targetCenter = target.getHitBox().getMidPoint();
int distanceSquared = baseCenter.distanceSquared(targetCenter);

String result = ChatColor.BLUE + target.getType().getStringProperty(CraftType.NAME);
if (result.length() > 9)
result = result.substring(0, 7);
Component result = Component.text(craftTypeName + " ").style(style);

result += " " + (int) Math.sqrt(distanceSquared);
int diffX = baseCenter.getX() - targetCenter.getX();
int diffZ = baseCenter.getZ() - targetCenter.getZ();
String directionStr = "" + (int) Math.sqrt(distanceSquared);
if (Math.abs(diffX) > Math.abs(diffZ)) {
if (diffX<0) {
result +=" E";
directionStr +=" E";
} else {
result +=" W";
directionStr +=" W";
}
} else {
if (diffZ<0) {
result +=" S";
directionStr +=" S";
} else {
result +=" N";
directionStr +=" N";
}
}
result = result.append(Component.text(directionStr).style(STYLE_COLOR_WHITE));
return result;
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onSignClickEvent(@NotNull PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK)
return;
@Override
protected boolean internalProcessSignWithCraft(Action clickType, AbstractSignListener.SignWrapper sign, Craft craft, Player player) {
player.performCommand("contacts");

Block block = event.getClickedBlock();
if (block == null)
return;
if (!(block.getState() instanceof Sign sign))
return;
return true;
}

@Override
protected @Nullable Component getUpdateString(int lineIndex, Component oldData, Craft craft) {
Craft contact = null;
List<Craft> contacts = craft.getDataTag(Craft.CONTACTS);
if (contacts.isEmpty() || contacts.size() <= lineIndex) {
return EMPTY;
}
contact = contacts.get(lineIndex);

return contactsLine(craft, contact);
}

@Override
protected @Nullable Component getDefaultString(int lineIndex, Component oldComponent) {
return EMPTY;
}

@Override
protected void performUpdate(Component[] newComponents, AbstractSignListener.SignWrapper sign, REFRESH_CAUSE refreshCause) {
for (int i = 0; i < newComponents.length; i++) {
Component newComp = newComponents[i];
if (newComp != null) {
sign.line(i, newComp);
}
}
if (refreshCause != REFRESH_CAUSE.SIGN_MOVED_BY_CRAFT && sign.block() != null) {
sign.block().update();
}
}

if (!ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase(HEADER))
return;
@Override
protected void onCraftIsBusy(Player player, Craft craft) {

event.setCancelled(true);
event.getPlayer().performCommand("contacts");
}
}
Loading