From f03a80cf99a22eb9f4bdf1ef799bb5be0d05319f Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 10 May 2021 14:02:20 +0200 Subject: [PATCH] Implemented teleporting --- .../ruakij/LinkedBeaconTeleporters/Main.java | 5 ++ .../listener/OnCustomPlayerMove.java | 82 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/listener/OnCustomPlayerMove.java diff --git a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/Main.java b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/Main.java index 9d3739f..b9c316d 100644 --- a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/Main.java +++ b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/Main.java @@ -1,9 +1,11 @@ package eu.ruekov.ruakij.LinkedBeaconTeleporters; +import eu.ruekov.ruakij.LinkedBeaconTeleporters.customPlayerMoveEvent.CustomPlayerMoveEventHandler; import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporterManager; import eu.ruekov.ruakij.LinkedBeaconTeleporters.listener.OnBlockBreak; import eu.ruekov.ruakij.LinkedBeaconTeleporters.listener.OnBlockPlace; import eu.ruekov.ruakij.LinkedBeaconTeleporters.listener.OnCraftItemEvent; +import eu.ruekov.ruakij.LinkedBeaconTeleporters.listener.OnCustomPlayerMove; import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; @@ -35,6 +37,9 @@ public class Main extends JavaPlugin { pluginManager.registerEvents(new OnBlockPlace(), this); pluginManager.registerEvents(new OnCraftItemEvent(), this); + CustomPlayerMoveEventHandler cpmHandler = new CustomPlayerMoveEventHandler(this, true); + cpmHandler.registerListener(new OnCustomPlayerMove()); + loadConfigs(); lbtManager = new LinkedBeaconTeleporterManager(); diff --git a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/listener/OnCustomPlayerMove.java b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/listener/OnCustomPlayerMove.java new file mode 100644 index 0000000..c732d7d --- /dev/null +++ b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/listener/OnCustomPlayerMove.java @@ -0,0 +1,82 @@ +package eu.ruekov.ruakij.LinkedBeaconTeleporters.listener; + +import eu.ruekov.ruakij.LinkedBeaconTeleporters.Function; +import eu.ruekov.ruakij.LinkedBeaconTeleporters.Main; +import eu.ruekov.ruakij.LinkedBeaconTeleporters.customPlayerMoveEvent.CustomPlayerMoveEvent; +import eu.ruekov.ruakij.LinkedBeaconTeleporters.customPlayerMoveEvent.CustomPlayerMoveEventListener; +import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporterBlock; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.util.Vector; + +import java.util.HashMap; +import java.util.List; +import java.util.UUID; + +public class OnCustomPlayerMove implements CustomPlayerMoveEventListener { + + static HashMap playerBeaconLocation = new HashMap<>(); + + @Override + public void tick(CustomPlayerMoveEvent e) { + } + + @Override + public void locationChange(CustomPlayerMoveEvent e) { + + Player p = e.player(); + UUID uuid = p.getUniqueId(); + + // If player found in ignoreList, remove + if(playerBeaconLocation.containsKey(uuid)) + playerBeaconLocation.remove(uuid); + + // Check if player is on-top of a beacon + Block block = Function.searchForMaterial( + p.getLocation().add(0, -1, 0), + new Vector(0, -1, 0), + Material.BEACON, + Function.transparentMaterials + ); + + // Skip if not found + if(block == null) return; + + // TODO: Check if beacon is active + + // Check if beacon is a LinkedBeaconTeleporter + LinkedBeaconTeleporterBlock lbtBlock = Main.lbtManager.getLbtBlockFromLocation(block.getLocation()); + + // Ignore if not found + if(lbtBlock == null) return; + + // Check if player should be ignored on this LinkedBeaconTeleporter + if(playerBeaconLocation.get(uuid) != null && playerBeaconLocation.get(uuid) == lbtBlock) return; + + // Find partnering Beacon + List lbtBlocks = Main.lbtManager.getLbtBlocksFromTeleportId(lbtBlock.teleporterId()); + if(lbtBlocks == null) return; + + // Get first partner thats not our current-block + LinkedBeaconTeleporterBlock lbtBlockPartner = null; + for(LinkedBeaconTeleporterBlock lbtBlockTmp: lbtBlocks){ + if(lbtBlockTmp != lbtBlocks){ + lbtBlockPartner = lbtBlockTmp; + break; + } + } + if(lbtBlockPartner == null){ + // No other partner + } + else{ + // Set player as ignored on target-LinkedBeaconTeleporter (so he wont trigger the teleport again) + playerBeaconLocation.put(uuid, lbtBlockPartner); + + // Teleport + e.player().teleport( + lbtBlockPartner.block().getLocation().add(0, 1, 0) + ); + } + } +}