From aa6b48fda8738361e00f44b3e7719f95c136c603 Mon Sep 17 00:00:00 2001 From: white_cola Date: Sat, 29 Jan 2022 16:38:01 +0800 Subject: [PATCH] EventHandlerAdapter upgrade, add shift-cancle feature to backRequest --- .../top/whitecola/coteleport/CoTeleport.java | 7 ++++- .../coteleport/commands/Tpaccept.java | 4 ++- .../coteleport/commands/Tpadeny.java | 4 ++- .../handler/CoTeleportFeatureHandler.java | 1 + .../handler/EventHandlerAdapter.java | 29 +++++++++++++++---- .../coteleport/handler/PlayerBackHandler.java | 12 ++++---- .../coteleport/interfaces/IEventHandler.java | 5 +++- .../coteleport/listeners/PlayerListener.java | 23 +++++++++------ .../coteleport/utils/HandlerFactory.java | 9 +++--- .../coteleport/utils/PlayerUtils.java | 3 +- .../coteleport/utils/TeleportUtils.java | 5 ++-- .../coteleport/wrapper/AbstractRequest.java | 2 +- .../coteleport/wrapper/BackRequest.java | 3 +- 13 files changed, 73 insertions(+), 34 deletions(-) diff --git a/src/main/java/top/whitecola/coteleport/CoTeleport.java b/src/main/java/top/whitecola/coteleport/CoTeleport.java index 213c8c6..dc297ef 100644 --- a/src/main/java/top/whitecola/coteleport/CoTeleport.java +++ b/src/main/java/top/whitecola/coteleport/CoTeleport.java @@ -24,7 +24,7 @@ public class CoTeleport extends JavaPlugin { { thread.start(); } - public PlayerTeleportEventHandler teleportEventHandler = new PlayerTeleportEventHandler(); + private PlayerTeleportEventHandler teleportEventHandler = new PlayerTeleportEventHandler(); private PlayerBackHandler playerBackHandler = new PlayerBackHandler(); @@ -79,4 +79,9 @@ public List onTabComplete(CommandSender sender, Command command, String public PlayerBackHandler getPlayerBackHandler() { return playerBackHandler; } + + + public PlayerTeleportEventHandler getTeleportEventHandler() { + return teleportEventHandler; + } } diff --git a/src/main/java/top/whitecola/coteleport/commands/Tpaccept.java b/src/main/java/top/whitecola/coteleport/commands/Tpaccept.java index 42a8e13..80c94ea 100644 --- a/src/main/java/top/whitecola/coteleport/commands/Tpaccept.java +++ b/src/main/java/top/whitecola/coteleport/commands/Tpaccept.java @@ -6,6 +6,8 @@ import top.whitecola.annotations.ItsACommand; import top.whitecola.commandhandler.ICommand; import top.whitecola.coteleport.CoTeleport; +import top.whitecola.coteleport.handler.PlayerTeleportEventHandler; +import top.whitecola.coteleport.utils.HandlerFactory; import top.whitecola.coteleport.wrapper.PlayerRequest; import java.util.Arrays; @@ -22,7 +24,7 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, if(args.length!=1) return false; - PlayerRequest toPlayerRequest = CoTeleport.instance.teleportEventHandler.getPlayerRequestByTo(toPlayer); + PlayerRequest toPlayerRequest = HandlerFactory.getHandler(PlayerTeleportEventHandler.class).getPlayerRequestByTo(toPlayer); if(toPlayerRequest==null){ toPlayer.sendMessage("§4你没有被传送请求,或该请求已过期。"); return true; diff --git a/src/main/java/top/whitecola/coteleport/commands/Tpadeny.java b/src/main/java/top/whitecola/coteleport/commands/Tpadeny.java index 6133071..e548253 100644 --- a/src/main/java/top/whitecola/coteleport/commands/Tpadeny.java +++ b/src/main/java/top/whitecola/coteleport/commands/Tpadeny.java @@ -6,6 +6,8 @@ import top.whitecola.annotations.ItsACommand; import top.whitecola.commandhandler.ICommand; import top.whitecola.coteleport.CoTeleport; +import top.whitecola.coteleport.handler.PlayerTeleportEventHandler; +import top.whitecola.coteleport.utils.HandlerFactory; import top.whitecola.coteleport.wrapper.PlayerRequest; import java.util.Arrays; @@ -23,7 +25,7 @@ public boolean onCommand(CommandSender commandSender, Command command, String s, if (args.length != 1) return false; - PlayerRequest toPlayerRequest = CoTeleport.instance.teleportEventHandler.getPlayerRequestByTo(toPlayer); + PlayerRequest toPlayerRequest = HandlerFactory.getHandler(PlayerTeleportEventHandler.class).getPlayerRequestByTo(toPlayer); if (toPlayerRequest == null) { toPlayer.sendMessage("§4你没有可以拒绝的请求,或请求已过期。"); diff --git a/src/main/java/top/whitecola/coteleport/handler/CoTeleportFeatureHandler.java b/src/main/java/top/whitecola/coteleport/handler/CoTeleportFeatureHandler.java index 8b0bc70..a0faf7f 100644 --- a/src/main/java/top/whitecola/coteleport/handler/CoTeleportFeatureHandler.java +++ b/src/main/java/top/whitecola/coteleport/handler/CoTeleportFeatureHandler.java @@ -1,4 +1,5 @@ package top.whitecola.coteleport.handler; public class CoTeleportFeatureHandler { + } diff --git a/src/main/java/top/whitecola/coteleport/handler/EventHandlerAdapter.java b/src/main/java/top/whitecola/coteleport/handler/EventHandlerAdapter.java index 201cc26..2297dcf 100644 --- a/src/main/java/top/whitecola/coteleport/handler/EventHandlerAdapter.java +++ b/src/main/java/top/whitecola/coteleport/handler/EventHandlerAdapter.java @@ -2,20 +2,39 @@ import top.whitecola.coteleport.interfaces.IEventHandler; import top.whitecola.coteleport.wrapper.AbstractRequest; +import top.whitecola.coteleport.wrapper.BackRequest; +import top.whitecola.coteleport.wrapper.PlayerRequest; public class EventHandlerAdapter implements IEventHandler { - @Override - public void stop() { - - } @Override public void addRequest(AbstractRequest request) { - + requests.add(request); } @Override public void removeRequest(AbstractRequest request) { + requests.remove(request); + } + + + public void addRequest(PlayerRequest playerRequest) { + addRequest((AbstractRequest) playerRequest); + } + + public void addRequest(BackRequest backRequest){ + addRequest((AbstractRequest) backRequest); + } + public void removeRequest(PlayerRequest playerRequest) { + removeRequest((AbstractRequest) playerRequest); } + +// public void removeRequest(BackRequest backRequest){ +// removeRequest((AbstractRequest) backRequest); +// +// } + + + } diff --git a/src/main/java/top/whitecola/coteleport/handler/PlayerBackHandler.java b/src/main/java/top/whitecola/coteleport/handler/PlayerBackHandler.java index c9e20a0..5acd93c 100644 --- a/src/main/java/top/whitecola/coteleport/handler/PlayerBackHandler.java +++ b/src/main/java/top/whitecola/coteleport/handler/PlayerBackHandler.java @@ -23,17 +23,15 @@ public Vector getBackRequests() { @Override public void removeRequest(AbstractRequest request) { - if(request instanceof AbstractRequest){ - backRequests.remove((BackRequest) request); - } + backRequests.remove((BackRequest) request); } public BackRequest getBackRequestByPlayer(Player player){ - for(int i=0;i requests = new Vector<>(); default void addRequest(AbstractRequest request){}; default void removeRequest(AbstractRequest request){}; + default void stop(){}; } diff --git a/src/main/java/top/whitecola/coteleport/listeners/PlayerListener.java b/src/main/java/top/whitecola/coteleport/listeners/PlayerListener.java index 0dcdda4..5dcaaf2 100644 --- a/src/main/java/top/whitecola/coteleport/listeners/PlayerListener.java +++ b/src/main/java/top/whitecola/coteleport/listeners/PlayerListener.java @@ -7,6 +7,10 @@ import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerToggleSneakEvent; import top.whitecola.coteleport.CoTeleport; +import top.whitecola.coteleport.handler.PlayerBackHandler; +import top.whitecola.coteleport.handler.PlayerTeleportEventHandler; +import top.whitecola.coteleport.utils.HandlerFactory; +import top.whitecola.coteleport.wrapper.AbstractRequest; import top.whitecola.coteleport.wrapper.BackRequest; import top.whitecola.coteleport.wrapper.PlayerRequest; @@ -17,7 +21,7 @@ public void playerMoveListener(PlayerMoveEvent e){ if(e.getFrom().getBlockX()==e.getTo().getBlockX() && e.getFrom().getBlockY()==e.getTo().getBlockY()&& e.getFrom().getBlockZ()==e.getTo().getBlockZ()){ return; } - PlayerRequest playerRequest = CoTeleport.instance.teleportEventHandler.getPlayerRequestByFrom(e.getPlayer()); + PlayerRequest playerRequest = HandlerFactory.getHandler(PlayerTeleportEventHandler.class).getPlayerRequestByFrom(e.getPlayer()); if(playerRequest==null) return; @@ -31,24 +35,25 @@ public void playerMoveListener(PlayerMoveEvent e){ @EventHandler public void PlayerShiftEvent(PlayerToggleSneakEvent e){ - PlayerRequest playerRequest = CoTeleport.instance.teleportEventHandler.getPlayerRequestByFrom(e.getPlayer()); - if(playerRequest==null) + AbstractRequest request = HandlerFactory.getHandler(PlayerTeleportEventHandler.class).getPlayerRequestByFrom(e.getPlayer()); + if(request==null && (request = HandlerFactory.getHandler(PlayerBackHandler.class).getBackRequestByPlayer(e.getPlayer()))==null) return; - if(playerRequest.getThread()==null) + + if(request.getThread()==null) return; - playerRequest.cancel(); + request.cancel(); } @EventHandler public void onPlayerQuitEvent(PlayerQuitEvent e){ - PlayerRequest playerRequest = CoTeleport.instance.teleportEventHandler.getPlayerRequestByFrom(e.getPlayer()); + PlayerRequest playerRequest = HandlerFactory.getHandler(PlayerTeleportEventHandler.class).getPlayerRequestByFrom(e.getPlayer()); if(playerRequest!=null){ - CoTeleport.instance.teleportEventHandler.getRequests().remove(playerRequest); - }else if((playerRequest = CoTeleport.instance.teleportEventHandler.getPlayerRequestByTo(e.getPlayer()))!=null){ - CoTeleport.instance.teleportEventHandler.getRequests().remove(playerRequest); + HandlerFactory.getHandler(PlayerTeleportEventHandler.class).getRequests().remove(playerRequest); + }else if((playerRequest = HandlerFactory.getHandler(PlayerTeleportEventHandler.class).getPlayerRequestByTo(e.getPlayer()))!=null){ + HandlerFactory.getHandler(PlayerTeleportEventHandler.class).getRequests().remove(playerRequest); } diff --git a/src/main/java/top/whitecola/coteleport/utils/HandlerFactory.java b/src/main/java/top/whitecola/coteleport/utils/HandlerFactory.java index 52a0c48..6ed3b3c 100644 --- a/src/main/java/top/whitecola/coteleport/utils/HandlerFactory.java +++ b/src/main/java/top/whitecola/coteleport/utils/HandlerFactory.java @@ -7,14 +7,15 @@ public class HandlerFactory { - public static T getHandler(Class handler){ - if(handler.getClass().equals(PlayerTeleportEventHandler.class)) - return (T) CoTeleport.instance.teleportEventHandler; - if(handler.getClass().equals(PlayerBackHandler.class)){ + if(handler.getSimpleName().equals(PlayerTeleportEventHandler.class.getSimpleName())) + return (T) CoTeleport.instance.getTeleportEventHandler(); + + + if(handler.getSimpleName().equals(PlayerBackHandler.class.getSimpleName())){ return (T) CoTeleport.instance.getPlayerBackHandler(); } diff --git a/src/main/java/top/whitecola/coteleport/utils/PlayerUtils.java b/src/main/java/top/whitecola/coteleport/utils/PlayerUtils.java index 5060bd9..98de423 100644 --- a/src/main/java/top/whitecola/coteleport/utils/PlayerUtils.java +++ b/src/main/java/top/whitecola/coteleport/utils/PlayerUtils.java @@ -7,9 +7,10 @@ public static boolean isSamePlayer(OfflinePlayer player, OfflinePlayer player2){ if(player==null || player2==null){ return false; } - if(player.getUniqueId().equals(player2.getUniqueId())){ + if(player.getUniqueId().toString().equals(player2.getUniqueId().toString())){ return true; } + return false; } } diff --git a/src/main/java/top/whitecola/coteleport/utils/TeleportUtils.java b/src/main/java/top/whitecola/coteleport/utils/TeleportUtils.java index dc9d1d7..c43f926 100644 --- a/src/main/java/top/whitecola/coteleport/utils/TeleportUtils.java +++ b/src/main/java/top/whitecola/coteleport/utils/TeleportUtils.java @@ -10,6 +10,7 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import top.whitecola.coteleport.CoTeleport; +import top.whitecola.coteleport.handler.PlayerTeleportEventHandler; import top.whitecola.coteleport.wrapper.AbstractRequest; import top.whitecola.coteleport.wrapper.PlayerNoticer; @@ -56,7 +57,7 @@ public static Thread tpaRequestHandle(AbstractRequest request, PlayerNoticer not try { Thread.sleep(1000); } catch (InterruptedException e) { - e.printStackTrace(); + return; } } @@ -70,7 +71,7 @@ public static Thread tpaRequestHandle(AbstractRequest request, PlayerNoticer not request.setTime(-1); - CoTeleport.instance.teleportEventHandler.getRequests().remove(request); + HandlerFactory.getHandler(PlayerTeleportEventHandler.class).getRequests().remove(request); return; } diff --git a/src/main/java/top/whitecola/coteleport/wrapper/AbstractRequest.java b/src/main/java/top/whitecola/coteleport/wrapper/AbstractRequest.java index 8a5cded..be0c2ca 100644 --- a/src/main/java/top/whitecola/coteleport/wrapper/AbstractRequest.java +++ b/src/main/java/top/whitecola/coteleport/wrapper/AbstractRequest.java @@ -38,8 +38,8 @@ public void deny() { @Override public void cancel() { setTime(-2); - addRequestOrRemoving(); thread.interrupt(); + addRequestOrRemoving(); } @Override diff --git a/src/main/java/top/whitecola/coteleport/wrapper/BackRequest.java b/src/main/java/top/whitecola/coteleport/wrapper/BackRequest.java index d605ba3..cc6b7d7 100644 --- a/src/main/java/top/whitecola/coteleport/wrapper/BackRequest.java +++ b/src/main/java/top/whitecola/coteleport/wrapper/BackRequest.java @@ -29,7 +29,7 @@ public void submitRequest() { @Override public void accept() { time = System.currentTimeMillis(); - TeleportUtils.tpaRequestHandle(this,new PlayerNoticer(from,null)); + thread = TeleportUtils.tpaRequestHandle(this,new PlayerNoticer(from,null)); } @Override @@ -41,6 +41,7 @@ public void deny() { @Override public void cancel() { + from.sendMessage("传送取消。"); super.cancel(); }