From cf061f9d75502ea849965f0218720ff10c3d8df4 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 10 May 2021 15:15:25 +0200 Subject: [PATCH] Migrated logic to objects --- .../LinkedBeaconTeleporter.java | 10 ++++ .../LinkedBeaconTeleporterBlock.java | 15 +++++- .../LinkedBeaconTeleporterItem.java | 23 +++++++- .../LinkedBeaconTeleporterManager.java | 52 +++---------------- 4 files changed, 51 insertions(+), 49 deletions(-) diff --git a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporter.java b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporter.java index 1a9ad03..83265b5 100644 --- a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporter.java +++ b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporter.java @@ -1,6 +1,7 @@ package eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter; import eu.ruekov.ruakij.LinkedBeaconTeleporters.Function; +import eu.ruekov.ruakij.LinkedBeaconTeleporters.Main; import org.bukkit.Location; import java.util.ArrayList; @@ -19,9 +20,18 @@ public abstract class LinkedBeaconTeleporter { LinkedBeaconTeleporter(){ this.teleporterId = Function.randomString("abcdefghijklmnopqrstuvwxyz0123456789", 10); + + linkedBeaconTeleporters = new ArrayList<>(); } public LinkedBeaconTeleporter(String teleporterId){ this.teleporterId = teleporterId; + + // Check if id exists + linkedBeaconTeleporters = Main.lbtManager.placedLBTsById.get(this.teleporterId()); + if(linkedBeaconTeleporters == null){ + // Create empty if not found + linkedBeaconTeleporters = new ArrayList<>(); + } } public String teleporterId(){ diff --git a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterBlock.java b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterBlock.java index ee34fcc..4525778 100644 --- a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterBlock.java +++ b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterBlock.java @@ -1,5 +1,6 @@ package eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter; +import eu.ruekov.ruakij.LinkedBeaconTeleporters.Function; import eu.ruekov.ruakij.LinkedBeaconTeleporters.Main; import org.bukkit.GameMode; import org.bukkit.Location; @@ -24,13 +25,23 @@ public class LinkedBeaconTeleporterBlock extends LinkedBeaconTeleporter { public LinkedBeaconTeleporterItem break_(BlockBreakEvent e){ + Location loc = this.block().getLocation(); + // Dont drop anything e.setDropItems(false); e.setExpToDrop(0); - LinkedBeaconTeleporterItem lbtItem = Main.lbtManager.breakLbtBlock(this); + // Remove from list + this.linkedBeaconTeleporters.remove(this); + // TODO: Remove from placedLBTsById when empty? - Location loc = this.block().getLocation(); + // Remove from loc + Main.lbtManager.placedLBTByLoc.remove( + Function.serialiseBlockLocation(loc) + ); + + // Item from block + LinkedBeaconTeleporterItem lbtItem = new LinkedBeaconTeleporterItem(this.teleporterId()); // Drop custom-item if(e.getPlayer().getGameMode() != GameMode.CREATIVE) diff --git a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterItem.java b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterItem.java index fc856d6..9f79d5d 100644 --- a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterItem.java +++ b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterItem.java @@ -1,13 +1,17 @@ package eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter; +import eu.ruekov.ruakij.LinkedBeaconTeleporters.Function; import eu.ruekov.ruakij.LinkedBeaconTeleporters.Main; import org.bukkit.GameMode; +import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.block.Block; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -39,7 +43,24 @@ public class LinkedBeaconTeleporterItem extends LinkedBeaconTeleporter { public LinkedBeaconTeleporterBlock place(BlockPlaceEvent e){ - LinkedBeaconTeleporterBlock lbtBlock = Main.lbtManager.placeLbtItem(this, e.getBlock()); + Block block = e.getBlock(); + Location loc = block.getLocation(); + + // Block from item + LinkedBeaconTeleporterBlock lbtBlock = new LinkedBeaconTeleporterBlock(this.teleporterId(), block); + // Add to list + this.linkedBeaconTeleporters.add(lbtBlock); + + // Add to id if not exists + if(!Main.lbtManager.placedLBTsById.containsKey(this.teleporterId)) + Main.lbtManager.placedLBTsById.put(this.teleporterId, this.linkedBeaconTeleporters); + + // Add to location + Main.lbtManager.placedLBTByLoc.put( + Function.serialiseBlockLocation(loc), + lbtBlock + ); + return lbtBlock; } diff --git a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterManager.java b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterManager.java index 583e8c3..f94bc37 100644 --- a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterManager.java +++ b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterManager.java @@ -19,8 +19,8 @@ import java.util.regex.Pattern; public class LinkedBeaconTeleporterManager { // Stores all placed Beacon-Teleporters for fast access // TODO: Evaluate necessity of 2 HashMaps with main reason of performance - HashMap> linkedBeaconTeleporterById = new HashMap(); - HashMap linkedBeaconTeleporterByLoc = new HashMap(); + public HashMap> placedLBTsById = new HashMap(); + public HashMap placedLBTByLoc = new HashMap(); // Regex to match item-name&lore and extract id (group 1) String regex; @@ -71,56 +71,16 @@ public class LinkedBeaconTeleporterManager { lbtBlock.linkedBeaconTeleporters = linkedBeaconTeleporters; // Save to loc-list - linkedBeaconTeleporterByLoc.put(serializedLoc, lbtBlock); + placedLBTByLoc.put(serializedLoc, lbtBlock); } // Save list to id-list - linkedBeaconTeleporterById.put(teleporterId, linkedBeaconTeleporters); + placedLBTsById.put(teleporterId, linkedBeaconTeleporters); } Main.log.info("All done!"); } - public LinkedBeaconTeleporterBlock placeLbtItem(LinkedBeaconTeleporterItem lbtItem, Block block){ - // Check if id already exists - List lbtBlocks = linkedBeaconTeleporterById.get(lbtItem.teleporterId()); - if(lbtBlocks == null){ - // Create empty if not found - lbtBlocks = new ArrayList<>(); - linkedBeaconTeleporterById.put(lbtItem.teleporterId(), lbtBlocks); - } - - // Block from item - LinkedBeaconTeleporterBlock lbtBlock = new LinkedBeaconTeleporterBlock(lbtItem.teleporterId(), block); - - // Add to list - lbtBlocks.add(lbtBlock); - // Add to location - linkedBeaconTeleporterByLoc.put( - Function.serialiseBlockLocation(block.getLocation()), - lbtBlock - ); - - return lbtBlock; - } - - public LinkedBeaconTeleporterItem breakLbtBlock(LinkedBeaconTeleporterBlock lbtBlock){ - // Get list by id - List lbtBlocks = linkedBeaconTeleporterById.get(lbtBlock.teleporterId()); - - // Remove from list - lbtBlocks.remove(lbtBlock); - // Remove from location - linkedBeaconTeleporterByLoc.remove( - Function.serialiseBlockLocation(lbtBlock.block().getLocation()) - ); - - // Item from block - LinkedBeaconTeleporterItem lbtItem = new LinkedBeaconTeleporterItem(lbtBlock.teleporterId()); - - return lbtItem; - } - void constructAndAddRecipes(){ // Create result-item ItemStack item = getItemStackFromData("-"); @@ -195,10 +155,10 @@ public class LinkedBeaconTeleporterManager { // Serialize String serializedLoc = Function.serialiseBlockLocation(loc); - return linkedBeaconTeleporterByLoc.get(serializedLoc); + return placedLBTByLoc.get(serializedLoc); } public List getLbtBlocksFromTeleportId(String teleportId){ - return linkedBeaconTeleporterById.get(teleportId); + return placedLBTsById.get(teleportId); } String constructRegex(String name, List lore){