Migrated logic to objects

master
Ruakij 4 years ago
parent b593a22568
commit cf061f9d75

@ -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(){

@ -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)

@ -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;
}

@ -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<String, List<LinkedBeaconTeleporter>> linkedBeaconTeleporterById = new HashMap();
HashMap<String, LinkedBeaconTeleporterBlock> linkedBeaconTeleporterByLoc = new HashMap();
public HashMap<String, List<LinkedBeaconTeleporter>> placedLBTsById = new HashMap();
public HashMap<String, LinkedBeaconTeleporterBlock> 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<LinkedBeaconTeleporter> 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<LinkedBeaconTeleporter> 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<LinkedBeaconTeleporter> getLbtBlocksFromTeleportId(String teleportId){
return linkedBeaconTeleporterById.get(teleportId);
return placedLBTsById.get(teleportId);
}
String constructRegex(String name, List<String> lore){

Loading…
Cancel
Save