Implemented partner-list saved on LinkedBeaconTeleporter

master
Ruakij 4 years ago
parent f03a80cf99
commit 3d602c526d

@ -3,6 +3,9 @@ package eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.Function; import eu.ruekov.ruakij.LinkedBeaconTeleporters.Function;
import org.bukkit.Location; import org.bukkit.Location;
import java.util.ArrayList;
import java.util.List;
/** /**
* Describes a placed-beacon-teleporter in the world * Describes a placed-beacon-teleporter in the world
*/ */
@ -11,6 +14,9 @@ public abstract class LinkedBeaconTeleporter {
// Persistent id for linked-beacons // Persistent id for linked-beacons
public String teleporterId; public String teleporterId;
// List of all linkedBeaconTeleporters with same teleporterId (real Type is usually ..Block, because linked Items are temporary constructs are useless)
protected List<LinkedBeaconTeleporter> linkedBeaconTeleporters = new ArrayList<>();
LinkedBeaconTeleporter(){ LinkedBeaconTeleporter(){
this.teleporterId = Function.randomString("abcdefghijklmnopqrstuvwxyz0123456789", 10); this.teleporterId = Function.randomString("abcdefghijklmnopqrstuvwxyz0123456789", 10);
} }
@ -21,4 +27,8 @@ public abstract class LinkedBeaconTeleporter {
public String teleporterId(){ public String teleporterId(){
return teleporterId; return teleporterId;
} }
public List<LinkedBeaconTeleporter> linkedBeaconTeleporters(){
return linkedBeaconTeleporters;
}
} }

@ -19,7 +19,7 @@ import java.util.regex.Pattern;
public class LinkedBeaconTeleporterManager { public class LinkedBeaconTeleporterManager {
// Stores all placed Beacon-Teleporters for fast access // Stores all placed Beacon-Teleporters for fast access
// TODO: Evaluate necessity of 2 HashMaps with main reason of performance // TODO: Evaluate necessity of 2 HashMaps with main reason of performance
HashMap<String, List<LinkedBeaconTeleporterBlock>> linkedBeaconTeleporterById = new HashMap(); HashMap<String, List<LinkedBeaconTeleporter>> linkedBeaconTeleporterById = new HashMap();
HashMap<String, LinkedBeaconTeleporterBlock> linkedBeaconTeleporterByLoc = new HashMap(); HashMap<String, LinkedBeaconTeleporterBlock> linkedBeaconTeleporterByLoc = new HashMap();
// Regex to match item-name&lore and extract id (group 1) // Regex to match item-name&lore and extract id (group 1)
@ -42,7 +42,7 @@ public class LinkedBeaconTeleporterManager {
ConfigurationSection teleportersData = Main.data.getConfigurationSection(teleporterId); ConfigurationSection teleportersData = Main.data.getConfigurationSection(teleporterId);
// New list for this id // New list for this id
ArrayList<LinkedBeaconTeleporterBlock> lbtBlocks = new ArrayList(); List<LinkedBeaconTeleporter> linkedBeaconTeleporters = new ArrayList();
for(String blockId : teleportersData.getKeys(false)) { for(String blockId : teleportersData.getKeys(false)) {
ConfigurationSection teleporterData = teleportersData.getConfigurationSection(blockId); ConfigurationSection teleporterData = teleportersData.getConfigurationSection(blockId);
@ -67,16 +67,15 @@ public class LinkedBeaconTeleporterManager {
// Construct block // Construct block
LinkedBeaconTeleporterBlock lbtBlock = new LinkedBeaconTeleporterBlock(teleporterId, block); LinkedBeaconTeleporterBlock lbtBlock = new LinkedBeaconTeleporterBlock(teleporterId, block);
// Write reference of list
// Add block to id-list lbtBlock.linkedBeaconTeleporters = linkedBeaconTeleporters;
lbtBlocks.add(lbtBlock);
// Save to loc-list // Save to loc-list
linkedBeaconTeleporterByLoc.put(serializedLoc, lbtBlock); linkedBeaconTeleporterByLoc.put(serializedLoc, lbtBlock);
} }
// Save list to id-list // Save list to id-list
linkedBeaconTeleporterById.put(teleporterId, lbtBlocks); linkedBeaconTeleporterById.put(teleporterId, linkedBeaconTeleporters);
} }
Main.log.info("All done!"); Main.log.info("All done!");
@ -84,7 +83,7 @@ public class LinkedBeaconTeleporterManager {
public LinkedBeaconTeleporterBlock placeLbtItem(LinkedBeaconTeleporterItem lbtItem, Block block){ public LinkedBeaconTeleporterBlock placeLbtItem(LinkedBeaconTeleporterItem lbtItem, Block block){
// Check if id already exists // Check if id already exists
List<LinkedBeaconTeleporterBlock> lbtBlocks = linkedBeaconTeleporterById.get(lbtItem.teleporterId()); List<LinkedBeaconTeleporter> lbtBlocks = linkedBeaconTeleporterById.get(lbtItem.teleporterId());
if(lbtBlocks == null){ if(lbtBlocks == null){
// Create empty if not found // Create empty if not found
lbtBlocks = new ArrayList<>(); lbtBlocks = new ArrayList<>();
@ -107,7 +106,7 @@ public class LinkedBeaconTeleporterManager {
public LinkedBeaconTeleporterItem breakLbtBlock(LinkedBeaconTeleporterBlock lbtBlock){ public LinkedBeaconTeleporterItem breakLbtBlock(LinkedBeaconTeleporterBlock lbtBlock){
// Get list by id // Get list by id
List<LinkedBeaconTeleporterBlock> lbtBlocks = linkedBeaconTeleporterById.get(lbtBlock.teleporterId()); List<LinkedBeaconTeleporter> lbtBlocks = linkedBeaconTeleporterById.get(lbtBlock.teleporterId());
// Remove from list // Remove from list
lbtBlocks.remove(lbtBlock); lbtBlocks.remove(lbtBlock);
@ -198,7 +197,7 @@ public class LinkedBeaconTeleporterManager {
return linkedBeaconTeleporterByLoc.get(serializedLoc); return linkedBeaconTeleporterByLoc.get(serializedLoc);
} }
public List<LinkedBeaconTeleporterBlock> getLbtBlocksFromTeleportId(String teleportId){ public List<LinkedBeaconTeleporter> getLbtBlocksFromTeleportId(String teleportId){
return linkedBeaconTeleporterById.get(teleportId); return linkedBeaconTeleporterById.get(teleportId);
} }

@ -4,6 +4,7 @@ import eu.ruekov.ruakij.LinkedBeaconTeleporters.Function;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.Main; import eu.ruekov.ruakij.LinkedBeaconTeleporters.Main;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.customPlayerMoveEvent.CustomPlayerMoveEvent; import eu.ruekov.ruakij.LinkedBeaconTeleporters.customPlayerMoveEvent.CustomPlayerMoveEvent;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.customPlayerMoveEvent.CustomPlayerMoveEventListener; import eu.ruekov.ruakij.LinkedBeaconTeleporters.customPlayerMoveEvent.CustomPlayerMoveEventListener;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporter;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporterBlock; import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporterBlock;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -52,17 +53,15 @@ public class OnCustomPlayerMove implements CustomPlayerMoveEventListener {
if(lbtBlock == null) return; if(lbtBlock == null) return;
// Check if player should be ignored on this LinkedBeaconTeleporter // Check if player should be ignored on this LinkedBeaconTeleporter
// FIXME: Will remove player from list, even though he should be ignored :/
if(playerBeaconLocation.get(uuid) != null && playerBeaconLocation.get(uuid) == lbtBlock) return; if(playerBeaconLocation.get(uuid) != null && playerBeaconLocation.get(uuid) == lbtBlock) return;
// Find partnering Beacon
List<LinkedBeaconTeleporterBlock> lbtBlocks = Main.lbtManager.getLbtBlocksFromTeleportId(lbtBlock.teleporterId());
if(lbtBlocks == null) return;
// Get first partner thats not our current-block // Get first partner thats not our current-block
LinkedBeaconTeleporterBlock lbtBlockPartner = null; LinkedBeaconTeleporterBlock lbtBlockPartner = null;
for(LinkedBeaconTeleporterBlock lbtBlockTmp: lbtBlocks){ for(LinkedBeaconTeleporter lbtBlockTmp: lbtBlock.linkedBeaconTeleporters()){
if(lbtBlockTmp != lbtBlocks){ // Upcast as linked-Teleporters are only blocks anyways
lbtBlockPartner = lbtBlockTmp; if((LinkedBeaconTeleporterBlock)lbtBlockTmp != lbtBlock){
lbtBlockPartner = (LinkedBeaconTeleporterBlock)lbtBlockTmp;
break; break;
} }
} }
@ -75,7 +74,7 @@ public class OnCustomPlayerMove implements CustomPlayerMoveEventListener {
// Teleport // Teleport
e.player().teleport( e.player().teleport(
lbtBlockPartner.block().getLocation().add(0, 1, 0) lbtBlockPartner.block().getLocation().add(0.5, 1, 0.5)
); );
} }
} }

Loading…
Cancel
Save