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] Sign rework api (part 1 of 2 for sign rework PR) #706

Open
wants to merge 56 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
a8a44db
sign rework - add API classes
DerToaster98 Aug 31, 2024
766cb8c
utility method to get Rotation by action
DerToaster98 Aug 31, 2024
2074d9d
necessary changes to SignTranslateEvent => use components (basically...)
DerToaster98 Aug 31, 2024
e4292f1
signListener implementations
DerToaster98 Aug 31, 2024
301b0bf
add instance for sign listener
DerToaster98 Aug 31, 2024
ff84ee8
add base signs (most basic signs that exist)
DerToaster98 Aug 31, 2024
7107633
forgot action import
DerToaster98 Aug 31, 2024
df4d4ef
fix getSignWrappers() method for sign => NPE
DerToaster98 Sep 5, 2024
f9e1ff6
corrections to BlockFace access
DerToaster98 Sep 5, 2024
b6bbdab
more fixes regarding getting the blockface of a sign...
DerToaster98 Sep 5, 2024
674c132
NPE prevention
DerToaster98 Sep 5, 2024
cb19e75
add TODO note
DerToaster98 Sep 5, 2024
bf4c7c9
remove spaces from suffix
DerToaster98 Sep 5, 2024
8422b22
Startup correction cherry picked
DerToaster98 Sep 5, 2024
221d0f2
correction => if the ident has trailing : => don't add them yourself …
DerToaster98 Sep 5, 2024
1e60c83
startup correction
DerToaster98 Sep 5, 2024
719a1ec
forgot the space
DerToaster98 Sep 5, 2024
956c8c5
crafttype is specified in second, not the third line
DerToaster98 Sep 5, 2024
8e1e7c7
typo
DerToaster98 Sep 5, 2024
4eda66f
fix information signs not updating
DerToaster98 Sep 6, 2024
d9afa5f
comment out debug logs
DerToaster98 Sep 6, 2024
19e2171
call update on the state, not on the block...
DerToaster98 Sep 6, 2024
6d8b47a
more elegant solution
DerToaster98 Sep 6, 2024
94ccf3c
add eventtype enum (to api classes)
DerToaster98 Sep 6, 2024
0a7fd09
refactor for eventtype enum (implementations (simple))
DerToaster98 Sep 6, 2024
a611a4f
supply eventType and use nullable for sign get access
DerToaster98 Sep 10, 2024
be724f9
just call it override
DerToaster98 Sep 10, 2024
8c0e50e
add registry base class, implement it, and use it
DerToaster98 Sep 10, 2024
8a2af8b
adjust comment
DerToaster98 Sep 10, 2024
0bfce90
adjust comment
DerToaster98 Sep 10, 2024
17b458e
remove old code
DerToaster98 Sep 10, 2024
4a6941a
remove 1.18 signListener
DerToaster98 Sep 11, 2024
000d987
fix build errors
DerToaster98 Sep 11, 2024
8349dd8
properly implement shouldCancel method (API + examples)
DerToaster98 Sep 11, 2024
e0f09d1
add override items and tools
DerToaster98 Sep 11, 2024
c68ff55
small tweaks regarding interactions
DerToaster98 Sep 11, 2024
e4ecbf9
NPE prevention
DerToaster98 Sep 11, 2024
6ab1baa
respect edits with dyecolors, wax, etc
DerToaster98 Sep 12, 2024
4a1f985
extract base elements from Cruise sign into a toggle sign
DerToaster98 Sep 12, 2024
b697375
update signs after detect
DerToaster98 Sep 12, 2024
b4f9032
cancel sign clicks on crafts in all cases
DerToaster98 Sep 12, 2024
35ea71e
only cancel when sign is empty
DerToaster98 Sep 12, 2024
9653b43
fix override param
DerToaster98 Sep 13, 2024
78d19f3
fix subcraft sign creation
DerToaster98 Sep 15, 2024
a4e21ec
Merge branch 'APDevTeam:main' into sign-rework-api
DerToaster98 Sep 17, 2024
889a5cf
Remote version specific SignListener implementations
DerToaster98 Sep 13, 2024
754f22e
adapt to refactoring
DerToaster98 Sep 13, 2024
ff1fd2f
use newer method
DerToaster98 Sep 13, 2024
ab2ebb7
fix compile errors and remove old code
DerToaster98 Sep 13, 2024
8c14dda
apply default text and implement processSignChange
DerToaster98 Sep 15, 2024
e864150
correct typo
DerToaster98 Sep 15, 2024
8f9ee61
return true
DerToaster98 Sep 17, 2024
c722fbb
call stuff for the other side, if that side is a movecraft sign
DerToaster98 Sep 17, 2024
ab7ae64
fix compile errors
DerToaster98 Sep 17, 2024
9d850ce
add getter for crafttype
DerToaster98 Sep 29, 2024
9d35183
check if player actually is the pilot of that craft
DerToaster98 Sep 30, 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
18 changes: 16 additions & 2 deletions Movecraft/src/main/java/net/countercraft/movecraft/Movecraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class Movecraft extends JavaPlugin {
private SmoothTeleport smoothTeleport;
private AsyncManager asyncManager;
private WreckManager wreckManager;
private AbstractSignListener abstractSignListener;

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

// Create instance of sign listener
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 @@ -213,22 +221,24 @@ public void onEnable() {
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 CraftPilotListener(), this);
getServer().getPluginManager().registerEvents(new CraftReleaseListener(), this);
getServer().getPluginManager().registerEvents(new CraftTypeListener(), this);

// Signs
AbstractMovecraftSign.register("Release", new ReleaseSign());

var contactsManager = new ContactsManager();
contactsManager.runTaskTimerAsynchronously(this, 0, 20);
Expand Down Expand Up @@ -340,4 +350,8 @@ public AsyncManager getAsyncManager() {
public @NotNull WreckManager getWreckManager(){
return wreckManager;
}

public AbstractSignListener getAbstractSignListener() {
return abstractSignListener;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import net.countercraft.movecraft.processing.effects.Effect;
import net.countercraft.movecraft.processing.functions.CraftSupplier;
import net.countercraft.movecraft.processing.tasks.detection.DetectionTask;
import net.countercraft.movecraft.sign.AbstractMovecraftSign;
import net.countercraft.movecraft.sign.CraftPilotSign;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -96,6 +98,9 @@ private CraftManager(boolean loadCraftTypes) {
craftTypes = loadCraftTypes();
else
craftTypes = new HashSet<>();

// Since the event is only created in reload cases...
AbstractMovecraftSign.registerCraftPilotSigns(CraftManager.getInstance().getCraftTypes(), CraftPilotSign::new);
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.countercraft.movecraft.listener;

import net.countercraft.movecraft.craft.CraftManager;
import net.countercraft.movecraft.events.TypesReloadedEvent;
import net.countercraft.movecraft.sign.AbstractMovecraftSign;
import net.countercraft.movecraft.sign.CraftPilotSign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

public class CraftTypeListener implements Listener {

@EventHandler
public void onReload(TypesReloadedEvent event) {
AbstractMovecraftSign.registerCraftPilotSigns(CraftManager.getInstance().getCraftTypes(), CraftPilotSign::new);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,89 +4,85 @@
import net.countercraft.movecraft.Movecraft;
import net.countercraft.movecraft.MovecraftLocation;
import net.countercraft.movecraft.config.Settings;
import net.countercraft.movecraft.craft.Craft;
import net.countercraft.movecraft.craft.CraftManager;
import net.countercraft.movecraft.craft.CruiseOnPilotCraft;
import net.countercraft.movecraft.craft.CruiseOnPilotSubCraft;
import net.countercraft.movecraft.craft.PlayerCraftImpl;
import net.countercraft.movecraft.craft.SubCraft;
import net.countercraft.movecraft.craft.*;
import net.countercraft.movecraft.craft.type.CraftType;
import net.countercraft.movecraft.events.CraftPilotEvent;
import net.countercraft.movecraft.events.CraftReleaseEvent;
import net.countercraft.movecraft.localisation.I18nSupport;
import net.countercraft.movecraft.processing.functions.Result;
import net.countercraft.movecraft.util.Pair;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.block.data.Directional;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

public final class CraftSign implements Listener {
private final Set<MovecraftLocation> piloting = new HashSet<>();
//TODO: This is not very pretty...
public class CraftPilotSign extends AbstractCraftPilotSign {

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onSignChange(@NotNull SignChangeEvent event) {
if (CraftManager.getInstance().getCraftTypeFromString(event.getLine(0)) == null)
return;
static final Set<MovecraftLocation> PILOTING = Collections.synchronizedSet(new HashSet<>());

if (!Settings.RequireCreatePerm)
return;

if (!event.getPlayer().hasPermission("movecraft." + ChatColor.stripColor(event.getLine(0)) + ".create")) {
event.getPlayer().sendMessage(I18nSupport.getInternationalisedString("Insufficient Permissions"));
event.setCancelled(true);
}
public CraftPilotSign(CraftType craftType) {
super(craftType);
}

@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onSignClick(@NotNull PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getClickedBlock() == null)
return;

BlockState state = event.getClickedBlock().getState();
if (!(state instanceof Sign))
return;

Sign sign = (Sign) state;
CraftType craftType = CraftManager.getInstance().getCraftTypeFromString(ChatColor.stripColor(sign.getLine(0)));
if (craftType == null)
return;
@Override
public boolean shouldCancelEvent(boolean processingSuccessful, @Nullable Action type, boolean sneaking) {
return processingSuccessful || !sneaking;
}

// Valid sign prompt for ship command.
event.setCancelled(true);
Player player = event.getPlayer();
if (!player.hasPermission("movecraft." + ChatColor.stripColor(sign.getLine(0)) + ".pilot")) {
@Override
protected boolean isSignValid(Action clickType, AbstractSignListener.SignWrapper sign, Player player) {
String header = sign.getRaw(0).trim();
CraftType craftType = CraftManager.getInstance().getCraftTypeFromString(header);
if (craftType != this.craftType) {
return false;
}
if (!player.hasPermission("movecraft." + header + ".pilot")) {
player.sendMessage(I18nSupport.getInternationalisedString("Insufficient Permissions"));
return;
return false;
} else {
return true;
}
}

Location loc = event.getClickedBlock().getLocation();
@Override
protected boolean internalProcessSign(Action clickType, AbstractSignListener.SignWrapper sign, Player player, @javax.annotation.Nullable Craft craft) {
if (this.craftType.getBoolProperty(CraftType.MUST_BE_SUBCRAFT) && craft == null) {
return false;
}
World world = sign.block().getWorld();
if (craft != null) {
world = craft.getWorld();
}
Location loc = sign.block().getLocation();
MovecraftLocation startPoint = new MovecraftLocation(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
if (piloting.contains(startPoint)) {
return;

if (PILOTING.contains(startPoint)) {
// Always return true
return true;
}

// Attempt to run detection
World world = event.getClickedBlock().getWorld();
runDetectTask(startPoint, player, sign, craft, world);

return true;
}

protected void runDetectTask(MovecraftLocation startPoint, Player player, AbstractSignListener.SignWrapper signWrapper, Craft parentCraft, World world) {
PILOTING.add(startPoint);
CraftManager.getInstance().detect(
startPoint,
craftType, (type, w, p, parents) -> {
// Assert instructions are not available normally, also this is checked in beforehand sort of
assert p != null; // Note: This only passes in a non-null player.
if (type.getBoolProperty(CraftType.CRUISE_ON_PILOT)) {
if (parents.size() > 1)
Expand Down Expand Up @@ -122,10 +118,12 @@ public void onSignClick(@NotNull PlayerInteractEvent event) {

if (craft.getType().getBoolProperty(CraftType.CRUISE_ON_PILOT)) {
// Setup cruise direction
if (sign.getBlockData() instanceof Directional)
BlockFace facing = signWrapper.facing();
craft.setCruiseDirection(CruiseDirection.fromBlockFace(facing));
/*if (signWrapper.block().getBlockData() instanceof Directional)
craft.setCruiseDirection(CruiseDirection.fromBlockFace(((Directional) sign.getBlockData()).getFacing()));
else
craft.setCruiseDirection(CruiseDirection.NONE);
craft.setCruiseDirection(CruiseDirection.NONE);*/
Copy link
Contributor

Choose a reason for hiding this comment

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

We can remove commented out code, Git keeps it around.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, will do that when i work on the fork again


// Start craft cruising
craft.setLastCruiseUpdate(System.currentTimeMillis());
Expand All @@ -148,11 +146,34 @@ public void run() {
}
}
);
// TODO: Move this to be directly called by the craftmanager post detection...
// Or use the event handler or something
new BukkitRunnable() {
@Override
public void run() {
piloting.remove(startPoint);
PILOTING.remove(startPoint);
}
}.runTaskLater(Movecraft.getInstance(), 4);

}

@Override
public boolean processSignChange(SignChangeEvent event, AbstractSignListener.SignWrapper sign) {
String header = sign.getRaw(0).trim();
CraftType craftType = CraftManager.getInstance().getCraftTypeFromString(header);
if (craftType != this.craftType) {
return false;
}
if (Settings.RequireCreatePerm) {
Player player = event.getPlayer();
if (!player.hasPermission("movecraft." + header + ".create")) {
player.sendMessage(I18nSupport.getInternationalisedString("Insufficient Permissions"));
return false;
} else {
return true;
}
} else {
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,44 @@
import net.countercraft.movecraft.craft.Craft;
import net.countercraft.movecraft.craft.CraftManager;
import net.countercraft.movecraft.events.CraftReleaseEvent;
import org.bukkit.ChatColor;
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 org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.jetbrains.annotations.NotNull;
import org.bukkit.event.block.SignChangeEvent;
import org.jetbrains.annotations.Nullable;

public final class ReleaseSign implements Listener{
private static final String HEADER = "Release";
public class ReleaseSign extends AbstractMovecraftSign {

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onSignClick(@NotNull PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
return;
}
BlockState state = event.getClickedBlock().getState();
if (!(state instanceof Sign)) {
return;
}
Sign sign = (Sign) state;
if (!ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase(HEADER)) {
return;
public ReleaseSign() {
super(null);
}

@Override
public boolean shouldCancelEvent(boolean processingSuccessful, @Nullable Action type, boolean sneaking) {
if (processingSuccessful) {
return true;
}
event.setCancelled(true);
Craft craft = CraftManager.getInstance().getCraftByPlayer(event.getPlayer());
return !sneaking;
}

@Override
protected boolean isSignValid(Action clickType, AbstractSignListener.SignWrapper sign, Player player) {
return true;
}

@Override
protected boolean internalProcessSign(Action clickType, AbstractSignListener.SignWrapper sign, Player player, @Nullable Craft craft) {
if (craft == null) {
return;
craft = CraftManager.getInstance().getCraftByPlayer(player);
}
if (craft != null) {
CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.PLAYER, false);
}
CraftManager.getInstance().release(craft, CraftReleaseEvent.Reason.PLAYER, false);
return true;
}

@Override
public boolean processSignChange(SignChangeEvent event, AbstractSignListener.SignWrapper sign) {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@

package net.countercraft.movecraft;

import org.bukkit.event.block.Action;

public enum MovecraftRotation {
CLOCKWISE, NONE, ANTICLOCKWISE
CLOCKWISE, NONE, ANTICLOCKWISE;

public static MovecraftRotation fromAction(Action clickType) {
switch (clickType) {
case LEFT_CLICK_AIR:
case LEFT_CLICK_BLOCK:
return ANTICLOCKWISE;
case RIGHT_CLICK_AIR:
case RIGHT_CLICK_BLOCK:
return CLOCKWISE;
default:
return NONE;
}
}
}
Loading