Skip to content

Commit

Permalink
EventHandlerAdapter upgrade, add shift-cancle feature to backRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
itscola committed Jan 29, 2022
1 parent 7b01fbf commit aa6b48f
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 34 deletions.
7 changes: 6 additions & 1 deletion src/main/java/top/whitecola/coteleport/CoTeleport.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();


Expand Down Expand Up @@ -79,4 +79,9 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
public PlayerBackHandler getPlayerBackHandler() {
return playerBackHandler;
}


public PlayerTeleportEventHandler getTeleportEventHandler() {
return teleportEventHandler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/top/whitecola/coteleport/commands/Tpadeny.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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你没有可以拒绝的请求,或请求已过期。");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package top.whitecola.coteleport.handler;

public class CoTeleportFeatureHandler {

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
//
// }



}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ public Vector<BackRequest> 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<backRequests.size();i++){
if(PlayerUtils.isSamePlayer(backRequests.get(i).getFrom(),player)){
return backRequests.get(i);
}
for(BackRequest backRequest : backRequests){

if(PlayerUtils.isSamePlayer(backRequest.getFrom(),player)){
return backRequest;
}
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import top.whitecola.coteleport.wrapper.AbstractRequest;

import java.util.Vector;

public interface IEventHandler {
default void stop(){};
Vector<AbstractRequest> requests = new Vector<>();
default void addRequest(AbstractRequest request){};
default void removeRequest(AbstractRequest request){};
default void stop(){};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

public class HandlerFactory {


public static <T extends EventHandlerAdapter> T getHandler(Class<T> 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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -56,7 +57,7 @@ public static Thread tpaRequestHandle(AbstractRequest request, PlayerNoticer not
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
return;
}
}

Expand All @@ -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;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public void deny() {
@Override
public void cancel() {
setTime(-2);
addRequestOrRemoving();
thread.interrupt();
addRequestOrRemoving();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,6 +41,7 @@ public void deny() {

@Override
public void cancel() {
from.sendMessage("传送取消。");
super.cancel();
}

Expand Down

0 comments on commit aa6b48f

Please sign in to comment.