Skip to content

Commit

Permalink
Back Feature is compelete, clean Teleporting code, added Teleporting …
Browse files Browse the repository at this point in the history
…class for teleporting in future.
  • Loading branch information
itscola committed Jan 30, 2022
1 parent aa6b48f commit 7f6953d
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 30 deletions.
Binary file modified libs/HiPlugin.jar
Binary file not shown.
18 changes: 6 additions & 12 deletions src/main/java/top/whitecola/coteleport/commands/Back.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import org.bukkit.entity.Player;
import top.whitecola.annotations.ItsACommand;
import top.whitecola.commandhandler.ICommand;
import top.whitecola.coteleport.CoTeleport;
import top.whitecola.coteleport.handler.PlayerBackHandler;
import top.whitecola.coteleport.utils.HandlerFactory;
import top.whitecola.coteleport.wrapper.BackRequest;
import top.whitecola.coteleport.utils.PlayerUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Vector;

@ItsACommand(CommandNmae = "back",premission = "ct.back")
public class Back implements ICommand {
Expand All @@ -22,15 +21,10 @@ public boolean onCommand(CommandSender commandSender, Command command, String s,

Player fromP = (Player) commandSender;

Vector<BackRequest> backRequests = CoTeleport.instance.getPlayerBackHandler().getBackRequests();
if(backRequests==null)
return false;

for(BackRequest backRequest : backRequests){
if(PlayerUtils.isSamePlayer(backRequest.getFrom(),fromP)){
backRequest.accept();
return true;
}
BackRequest backRequest;
if((backRequest = HandlerFactory.getHandler(PlayerBackHandler.class).getBackRequestByPlayer(fromP))!=null){
backRequest.accept();
return true;
}

fromP.sendMessage("§4上一个地点已过期或不存在,无法传送。");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package top.whitecola.coteleport.effects;

import de.slikey.effectlib.effect.CircleEffect;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import top.whitecola.coteleport.wrapper.AbstractRequest;
import top.whitecola.coteleport.wrapper.PlayerNoticer;

public abstract class AbstractTeleporting {

public void teleportToSomeplace(Entity entity, Location to){

}


public Thread tpaRequestHandle(AbstractRequest request, PlayerNoticer noticer){

return null;
}

public static CircleEffect playTeleportingParticle(Location location){

return null;
}


}
19 changes: 19 additions & 0 deletions src/main/java/top/whitecola/coteleport/effects/Teleporting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package top.whitecola.coteleport.effects;

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import top.whitecola.coteleport.wrapper.AbstractRequest;
import top.whitecola.coteleport.wrapper.PlayerNoticer;

public class Teleporting extends AbstractTeleporting{
@Override
public void teleportToSomeplace(Entity entity, Location to) {
super.teleportToSomeplace(entity, to);
}


@Override
public Thread tpaRequestHandle(AbstractRequest request, PlayerNoticer noticer) {
return super.tpaRequestHandle(request, noticer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ 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 = HandlerFactory.getHandler(PlayerTeleportEventHandler.class).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;

if(request.getTime()==-2 || request.getTime()==-1)
return;

e.setCancelled(true);
Expand All @@ -39,7 +43,6 @@ public void PlayerShiftEvent(PlayerToggleSneakEvent e){
if(request==null && (request = HandlerFactory.getHandler(PlayerBackHandler.class).getBackRequestByPlayer(e.getPlayer()))==null)
return;


if(request.getThread()==null)
return;

Expand All @@ -57,10 +60,12 @@ public void onPlayerQuitEvent(PlayerQuitEvent e){
}


BackRequest backRequest = CoTeleport.instance.getPlayerBackHandler().getBackRequestByPlayer(e.getPlayer());
BackRequest backRequest = HandlerFactory.getHandler(PlayerBackHandler.class).getBackRequestByPlayer(e.getPlayer());
if(backRequest!=null){
backRequest.cancel();
backRequest.addRequestOrRemoving();
}

return;
}

Expand Down
20 changes: 10 additions & 10 deletions src/main/java/top/whitecola/coteleport/utils/TeleportUtils.java
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.PlayerBackHandler;
import top.whitecola.coteleport.handler.PlayerTeleportEventHandler;
import top.whitecola.coteleport.wrapper.AbstractRequest;
import top.whitecola.coteleport.wrapper.PlayerNoticer;
Expand Down Expand Up @@ -45,6 +46,13 @@ public static Thread tpaRequestHandle(AbstractRequest request, PlayerNoticer not

if((request.getTime()!=-2)&&(System.currentTimeMillis()-request.getTime())>=3000){
request.setTime(-1);
teleportPlace(request.getFrom(), request.getTolcation());

noticer.getP1().spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent("§a§l传送完成!"));

if(noticer.getP2()!=null){
noticer.getP2().spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent("§a§l传送完成!"));
}
break;
}

Expand All @@ -55,23 +63,15 @@ public static Thread tpaRequestHandle(AbstractRequest request, PlayerNoticer not
noticer.getP1().spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent("§a§l即将传送,期间无法移动。按 SHIFT 以取消传送。"));

try {
Thread.sleep(1000);
Thread.sleep(10);
} catch (InterruptedException e) {
return;
}
}

teleportPlace(request.getFrom(), request.getTolcation());

noticer.getP1().spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent("§a§l传送完成!"));

if(noticer.getP2()!=null){
noticer.getP2().spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent("§a§l传送完成!"));
}


request.setTime(-1);
HandlerFactory.getHandler(PlayerTeleportEventHandler.class).getRequests().remove(request);
HandlerFactory.getHandler(PlayerBackHandler.class).getBackRequests().remove(request);
return;

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

if(!(this instanceof BackRequest))
addRequestOrRemoving();

thread = null;
}

@Override
Expand Down Expand Up @@ -90,7 +95,7 @@ public void setThread(Thread thread) {
this.thread = thread;
}

private void addRequestOrRemoving(){
public void addRequestOrRemoving(){
if(this instanceof PlayerRequest) {
if(getThread()==null && time!=-2){
HandlerFactory.getHandler(PlayerTeleportEventHandler.class).addRequest(this);
Expand Down

0 comments on commit 7f6953d

Please sign in to comment.