Compare commits

..

No commits in common. "6c2e06afaf58ad4801d3c10aaf00f30000d88e24" and "2f64eb5ed0e8dda9c86845a1c03860c837778a74" have entirely different histories.

9 changed files with 51 additions and 92 deletions

View File

@ -7,7 +7,10 @@ import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import java.util.*; import java.util.Arrays;
import java.util.InvalidPropertiesFormatException;
import java.util.List;
import java.util.Random;
public class Function { public class Function {
public static String serialiseBlockLocation(Location loc){ public static String serialiseBlockLocation(Location loc){
@ -126,9 +129,6 @@ public class Function {
Material.STONE_SLAB, Material.STONE_SLAB,
Material.WARPED_SLAB Material.WARPED_SLAB
); );
public static Block searchForMaterial(Location startLoc, Vector searchDirection, Material searchMaterial){
return searchForMaterial(startLoc, searchDirection, searchMaterial, new ArrayList<>());
}
public static Block searchForMaterial(Location startLoc, Vector searchDirection, Material searchMaterial, List<Material> ignoreMaterials){ public static Block searchForMaterial(Location startLoc, Vector searchDirection, Material searchMaterial, List<Material> ignoreMaterials){
Location loc = new Location(startLoc.getWorld(), startLoc.getBlockX(), startLoc.getBlockY(), startLoc.getBlockZ()); Location loc = new Location(startLoc.getWorld(), startLoc.getBlockX(), startLoc.getBlockY(), startLoc.getBlockZ());
for(int i=0; loc.getBlockY()>=0 && loc.getBlockY()<=255; loc.add(searchDirection)){ for(int i=0; loc.getBlockY()>=0 && loc.getBlockY()<=255; loc.add(searchDirection)){

View File

@ -25,6 +25,8 @@ public class Main extends JavaPlugin {
public static Logger log; public static Logger log;
public static LinkedBeaconTeleporterManager lbtManager;
public void onEnable() { public void onEnable() {
log = getLogger(); log = getLogger();
@ -40,15 +42,13 @@ public class Main extends JavaPlugin {
loadConfigs(); loadConfigs();
LinkedBeaconTeleporterManager.init(); lbtManager = new LinkedBeaconTeleporterManager();
log.info("Plugin activated"); log.info("Plugin activated");
} }
public void onDisable() { public void onDisable() {
LinkedBeaconTeleporterManager.saveData();
log.info("Plugin deactivated"); log.info("Plugin deactivated");
} }
@ -70,15 +70,4 @@ public class Main extends JavaPlugin {
Bukkit.getPluginManager().disablePlugin(this); Bukkit.getPluginManager().disablePlugin(this);
} }
} }
public static void saveData() {
// data.yml
try{
File dataFile = new File("plugins/"+ plugin.getName() +"/data.yml");
data.save(dataFile);
}catch (Exception ex){
log.severe("Could not save data.yml!");
ex.printStackTrace();
}
}
} }

View File

@ -3,6 +3,8 @@ package eu.ruekov.ruakij.LinkedBeaconTeleporters.customPlayerMoveEvent;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.io.InvalidObjectException;
public class CustomPlayerMoveEvent { public class CustomPlayerMoveEvent {
Player p; Player p;

View File

@ -27,7 +27,7 @@ public abstract class LinkedBeaconTeleporter {
this.teleporterId = teleporterId; this.teleporterId = teleporterId;
// Check if id exists // Check if id exists
linkedBeaconTeleporters = LinkedBeaconTeleporterManager.placedLBTsById.get(this.teleporterId()); linkedBeaconTeleporters = Main.lbtManager.placedLBTsById.get(this.teleporterId());
if(linkedBeaconTeleporters == null){ if(linkedBeaconTeleporters == null){
// Create empty if not found // Create empty if not found
linkedBeaconTeleporters = new ArrayList<>(); linkedBeaconTeleporters = new ArrayList<>();

View File

@ -31,21 +31,12 @@ public class LinkedBeaconTeleporterBlock extends LinkedBeaconTeleporter {
e.setDropItems(false); e.setDropItems(false);
e.setExpToDrop(0); e.setExpToDrop(0);
// Notify Player when link was destroyed
if(this.linkedBeaconTeleporters.size()-1 != 2){
e.getPlayer().sendMessage("§cConnection to Teleporter with id §7"+ this.teleporterId +" §e("+
(int)((LinkedBeaconTeleporterBlock)this.linkedBeaconTeleporters.get(0)).block.getLocation().distance(
((LinkedBeaconTeleporterBlock)this.linkedBeaconTeleporters.get(1)).block().getLocation()
)
+" Blocks away) §cdestroyed");
}
// Remove from list // Remove from list
this.linkedBeaconTeleporters.remove(this); this.linkedBeaconTeleporters.remove(this);
// TODO: Remove from placedLBTsById when empty? // TODO: Remove from placedLBTsById when empty?
// Remove from loc // Remove from loc
LinkedBeaconTeleporterManager.placedLBTByLoc.remove( Main.lbtManager.placedLBTByLoc.remove(
Function.serialiseBlockLocation(loc) Function.serialiseBlockLocation(loc)
); );
@ -59,15 +50,24 @@ public class LinkedBeaconTeleporterBlock extends LinkedBeaconTeleporter {
lbtItem.item() lbtItem.item()
); );
// Notify Player when link was destroyed
if(this.linkedBeaconTeleporters.size() < 2){
e.getPlayer().sendMessage("§cConnection to Teleporter with id §7"+ this.teleporterId +" §e("+
(int)((LinkedBeaconTeleporterBlock)this.linkedBeaconTeleporters.get(0)).block.getLocation().distance(
((LinkedBeaconTeleporterBlock)this.linkedBeaconTeleporters.get(1)).block().getLocation()
)
+" Blocks away) §cdestroyed");
}
return lbtItem; return lbtItem;
} }
// Conversion methods // Conversion methods
public static LinkedBeaconTeleporterBlock getFromLocation(Location loc){ public static LinkedBeaconTeleporterBlock getFromLocation(Location loc){
return LinkedBeaconTeleporterManager.getLbtBlockFromLocation(loc); return Main.lbtManager.getLbtBlockFromLocation(loc);
} }
public static List<LinkedBeaconTeleporter> getListFromTeleportId(String teleportId){ public static List<LinkedBeaconTeleporter> getListFromTeleportId(String teleportId){
return LinkedBeaconTeleporterManager.getLbtBlocksFromTeleportId(teleportId); return Main.lbtManager.getLbtBlocksFromTeleportId(teleportId);
} }
} }

View File

@ -24,7 +24,7 @@ public class LinkedBeaconTeleporterItem extends LinkedBeaconTeleporter {
// Create a new LinkedBeaconTeleporter // Create a new LinkedBeaconTeleporter
super(); super();
this.item = LinkedBeaconTeleporterManager.getItemStackFromData(this.teleporterId); this.item = Main.lbtManager.getItemStackFromData(this.teleporterId);
} }
public LinkedBeaconTeleporterItem(String teleporterId, ItemStack item) { public LinkedBeaconTeleporterItem(String teleporterId, ItemStack item) {
super(teleporterId); super(teleporterId);
@ -34,7 +34,7 @@ public class LinkedBeaconTeleporterItem extends LinkedBeaconTeleporter {
public LinkedBeaconTeleporterItem(String teleporterId){ public LinkedBeaconTeleporterItem(String teleporterId){
super(teleporterId); super(teleporterId);
this.item = LinkedBeaconTeleporterManager.getItemStackFromData(teleporterId); this.item = Main.lbtManager.getItemStackFromData(teleporterId);
} }
public ItemStack item(){ public ItemStack item(){
@ -60,17 +60,17 @@ public class LinkedBeaconTeleporterItem extends LinkedBeaconTeleporter {
this.linkedBeaconTeleporters.add(lbtBlock); this.linkedBeaconTeleporters.add(lbtBlock);
// Add to id if not exists // Add to id if not exists
if(!LinkedBeaconTeleporterManager.placedLBTsById.containsKey(this.teleporterId)) if(!Main.lbtManager.placedLBTsById.containsKey(this.teleporterId))
LinkedBeaconTeleporterManager.placedLBTsById.put(this.teleporterId, this.linkedBeaconTeleporters); Main.lbtManager.placedLBTsById.put(this.teleporterId, this.linkedBeaconTeleporters);
// Add to location // Add to location
LinkedBeaconTeleporterManager.placedLBTByLoc.put( Main.lbtManager.placedLBTByLoc.put(
Function.serialiseBlockLocation(loc), Function.serialiseBlockLocation(loc),
lbtBlock lbtBlock
); );
// Notify Player when no other teleporter was found (yet?) // Notify Player when no other teleporter was found (yet?)
if(this.linkedBeaconTeleporters.size() == 1){ if(this.linkedBeaconTeleporters.size() == 2){
e.getPlayer().sendMessage("§6No other Teleporter found with with id §7"+ this.teleporterId); e.getPlayer().sendMessage("§6No other Teleporter found with with id §7"+ this.teleporterId);
} }
// Notify Player when we have a full link // Notify Player when we have a full link
@ -88,6 +88,6 @@ public class LinkedBeaconTeleporterItem extends LinkedBeaconTeleporter {
// Conversion methods // Conversion methods
public static LinkedBeaconTeleporterItem getFromItemStack(ItemStack itemStack){ public static LinkedBeaconTeleporterItem getFromItemStack(ItemStack itemStack){
return LinkedBeaconTeleporterManager.getLbtItemFromItemStack(itemStack); return Main.lbtManager.getLbtItemFromItemStack(itemStack);
} }
} }

View File

@ -7,7 +7,7 @@ import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapelessRecipe; import org.bukkit.inventory.ShapelessRecipe;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
@ -19,13 +19,13 @@ 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
public static HashMap<String, List<LinkedBeaconTeleporter>> placedLBTsById = new HashMap(); public HashMap<String, List<LinkedBeaconTeleporter>> placedLBTsById = new HashMap();
public static HashMap<String, LinkedBeaconTeleporterBlock> placedLBTByLoc = new HashMap(); public HashMap<String, LinkedBeaconTeleporterBlock> placedLBTByLoc = new HashMap();
// Regex to match item-name&lore and extract id (group 1) // Regex to match item-name&lore and extract id (group 1)
static String regex; String regex;
public static void init(){ public LinkedBeaconTeleporterManager(){
Main.log.info("Loading LinkedBeaconTeleporter from config.."); Main.log.info("Loading LinkedBeaconTeleporter from config..");
// Construct search item-regex // Construct search item-regex
@ -39,19 +39,21 @@ public class LinkedBeaconTeleporterManager {
// Load teleporters // Load teleporters
for(String teleporterId : Main.data.getKeys(false)){ for(String teleporterId : Main.data.getKeys(false)){
List<String> serialisedLocs = Main.data.getStringList(teleporterId); ConfigurationSection teleportersData = Main.data.getConfigurationSection(teleporterId);
// New list for this id // New list for this id
List<LinkedBeaconTeleporter> linkedBeaconTeleporters = new ArrayList(); List<LinkedBeaconTeleporter> linkedBeaconTeleporters = new ArrayList();
for(String serializedLoc : serialisedLocs) { for(String blockId : teleportersData.getKeys(false)) {
ConfigurationSection teleporterData = teleportersData.getConfigurationSection(blockId);
// Load location // Load location
String serializedLoc = teleporterData.getString("loc");
Location loc; Location loc;
try { try {
loc = Function.deserializeBlockLocation(serializedLoc); loc = Function.deserializeBlockLocation(teleporterData.getString("loc"));
} catch (InvalidPropertiesFormatException e) { } catch (InvalidPropertiesFormatException e) {
Main.log.severe("Could not load location='"+ serializedLoc +"' from "+ teleporterId); Main.log.severe("Could not load location='"+ serializedLoc +"' from "+ teleporterId +" > "+ blockId);
continue; continue;
} }
@ -59,7 +61,7 @@ public class LinkedBeaconTeleporterManager {
Block block = loc.getBlock(); Block block = loc.getBlock();
if(block.getType() != Material.BEACON){ if(block.getType() != Material.BEACON){
// Data out of sync! Skipping // Data out of sync! Skipping
Main.log.warning(teleporterId +" at location "+ serializedLoc +" changed and is no longer LinkedBlockTeleporter! (Changed without the plugin running?)"); Main.log.warning(teleporterId +" > "+ blockId +" at location "+ serializedLoc +" changed and is no longer LinkedBlockTeleporter! (Changed without the plugin running?)");
continue; continue;
} }
@ -79,32 +81,7 @@ public class LinkedBeaconTeleporterManager {
Main.log.info("All done!"); Main.log.info("All done!");
} }
public static void saveData(){ void constructAndAddRecipes(){
Main.log.info("Saving data..");
Main.data = new YamlConfiguration();
for(String teleporterId : placedLBTsById.keySet()){
List<LinkedBeaconTeleporter> LBTs = placedLBTsById.get(teleporterId);
// Generate clean array with data we want
List<String> dataList = new ArrayList<>();
for(LinkedBeaconTeleporter lbt : LBTs){
dataList.add(
Function.serialiseBlockLocation(
((LinkedBeaconTeleporterBlock)lbt).block().getLocation()
)
);
}
Main.data.set(teleporterId, dataList);
}
Main.saveData();
Main.log.info("done");
}
static void constructAndAddRecipes(){
// Create result-item // Create result-item
ItemStack item = getItemStackFromData("-"); ItemStack item = getItemStackFromData("-");
item.setAmount(2); item.setAmount(2);
@ -126,7 +103,7 @@ public class LinkedBeaconTeleporterManager {
} }
// Conversion-methods // Conversion-methods
public static ItemStack getItemStackFromData(String teleporterId){ public ItemStack getItemStackFromData(String teleporterId){
ItemStack item = new ItemStack(Material.BEACON); ItemStack item = new ItemStack(Material.BEACON);
ItemMeta itemMeta = item.getItemMeta(); ItemMeta itemMeta = item.getItemMeta();
@ -150,7 +127,7 @@ public class LinkedBeaconTeleporterManager {
return item; return item;
} }
public static LinkedBeaconTeleporterItem getLbtItemFromItemStack(ItemStack itemStack){ public LinkedBeaconTeleporterItem getLbtItemFromItemStack(ItemStack itemStack){
ItemMeta itemMeta = itemStack.getItemMeta(); ItemMeta itemMeta = itemStack.getItemMeta();
// Serialize // Serialize
@ -174,17 +151,17 @@ public class LinkedBeaconTeleporterManager {
return lbtItem; return lbtItem;
} }
public static LinkedBeaconTeleporterBlock getLbtBlockFromLocation(Location loc){ public LinkedBeaconTeleporterBlock getLbtBlockFromLocation(Location loc){
// Serialize // Serialize
String serializedLoc = Function.serialiseBlockLocation(loc); String serializedLoc = Function.serialiseBlockLocation(loc);
return placedLBTByLoc.get(serializedLoc); return placedLBTByLoc.get(serializedLoc);
} }
public static List<LinkedBeaconTeleporter> getLbtBlocksFromTeleportId(String teleportId){ public List<LinkedBeaconTeleporter> getLbtBlocksFromTeleportId(String teleportId){
return placedLBTsById.get(teleportId); return placedLBTsById.get(teleportId);
} }
static String constructRegex(String name, List<String> lore){ String constructRegex(String name, List<String> lore){
// Serialize // Serialize
String regex = serialiseBeaconItem(name, lore); String regex = serialiseBeaconItem(name, lore);
@ -198,7 +175,7 @@ public class LinkedBeaconTeleporterManager {
return regex; return regex;
} }
static String serialiseBeaconItem(String name, List<String> lore){ String serialiseBeaconItem(String name, List<String> lore){
// get name and remove control-chars // get name and remove control-chars
String str = name.replace("\\", "\\\\"); String str = name.replace("\\", "\\\\");
if(lore != null){ if(lore != null){

View File

@ -2,7 +2,6 @@ package eu.ruekov.ruakij.LinkedBeaconTeleporters.listener;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.Main; import eu.ruekov.ruakij.LinkedBeaconTeleporters.Main;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporterItem; import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporterItem;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporterManager;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -22,7 +21,7 @@ public class OnCraftItemEvent implements Listener {
ItemStack itemResult = recipe.getResult(); ItemStack itemResult = recipe.getResult();
// Check if it has LinkedBeaconTeleporter-Data // Check if it has LinkedBeaconTeleporter-Data
LinkedBeaconTeleporterItem lbtItem = LinkedBeaconTeleporterManager.getLbtItemFromItemStack(itemResult); LinkedBeaconTeleporterItem lbtItem = Main.lbtManager.getLbtItemFromItemStack(itemResult);
// Ignore if not found // Ignore if not found
if(lbtItem == null) return; if(lbtItem == null) return;

View File

@ -6,7 +6,6 @@ import eu.ruekov.ruakij.LinkedBeaconTeleporters.customPlayerMoveEvent.CustomPlay
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.LinkedBeaconTeleporter;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporterBlock; import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporterBlock;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporterManager;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -58,7 +57,7 @@ public class OnCustomPlayerMove implements CustomPlayerMoveEventListener {
// TODO: Check if beacon is active // TODO: Check if beacon is active
// Check if beacon is a LinkedBeaconTeleporter // Check if beacon is a LinkedBeaconTeleporter
LinkedBeaconTeleporterBlock lbtBlock = LinkedBeaconTeleporterManager.getLbtBlockFromLocation(block.getLocation()); LinkedBeaconTeleporterBlock lbtBlock = Main.lbtManager.getLbtBlockFromLocation(block.getLocation());
// Ignore if not found // Ignore if not found
if(lbtBlock == null) return; if(lbtBlock == null) return;
@ -85,16 +84,9 @@ public class OnCustomPlayerMove implements CustomPlayerMoveEventListener {
// Set player as ignored on target-LinkedBeaconTeleporter (so he wont trigger the teleport again) // Set player as ignored on target-LinkedBeaconTeleporter (so he wont trigger the teleport again)
playerBeaconLocation.put(uuid, lbtBlockPartner); playerBeaconLocation.put(uuid, lbtBlockPartner);
// Get safe-location on top of other beacon
Block safeBlock = Function.searchForMaterial(
lbtBlockPartner.block().getLocation().add(0, 1, 0),
new Vector(0, 1, 0),
Material.AIR
);
// Teleport // Teleport
e.player().teleport( e.player().teleport(
safeBlock.getLocation().add(0.5, 0, 0.5) lbtBlockPartner.block().getLocation().add(0.5, 1, 0.5)
); );
} }
} }