Compare commits

...

8 Commits

Author SHA1 Message Date
Ruakij
2f64eb5ed0 Added player-messages 2021-05-10 15:23:29 +02:00
Ruakij
cf061f9d75 Migrated logic to objects 2021-05-10 15:15:25 +02:00
Ruakij
b593a22568 Moved code to objects 2021-05-10 14:51:14 +02:00
Ruakij
884184006c Added player to ignoreList even on unsuccessful teleport 2021-05-10 14:30:49 +02:00
Ruakij
41a15103b3 Fixed List having wrong Type 2021-05-10 14:24:29 +02:00
Ruakij
b010280111 Fixed player being removed from ignoreList directly after teleport. 2021-05-10 14:23:53 +02:00
Ruakij
63bab8ae2e Fixed misplaced FIXME comment 2021-05-10 14:19:27 +02:00
Ruakij
3d602c526d Implemented partner-list saved on LinkedBeaconTeleporter 2021-05-10 14:18:11 +02:00
7 changed files with 148 additions and 79 deletions

View File

@ -1,8 +1,12 @@
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;
import java.util.List;
/**
* Describes a placed-beacon-teleporter in the world
*/
@ -11,14 +15,30 @@ public abstract class LinkedBeaconTeleporter {
// Persistent id for linked-beacons
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(){
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(){
return teleporterId;
}
public List<LinkedBeaconTeleporter> linkedBeaconTeleporters(){
return linkedBeaconTeleporters;
}
}

View File

@ -1,10 +1,11 @@
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.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.inventory.ItemStack;
import org.bukkit.event.block.BlockBreakEvent;
import java.util.List;
@ -23,11 +24,50 @@ public class LinkedBeaconTeleporterBlock extends LinkedBeaconTeleporter {
}
public LinkedBeaconTeleporterItem break_(BlockBreakEvent e){
Location loc = this.block().getLocation();
// Dont drop anything
e.setDropItems(false);
e.setExpToDrop(0);
// Remove from list
this.linkedBeaconTeleporters.remove(this);
// TODO: Remove from placedLBTsById when empty?
// 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)
loc.getWorld().dropItemNaturally(
loc.add(0.5, 0, 0.5),
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;
}
// Conversion methods
public static LinkedBeaconTeleporterBlock getFromLocation(Location loc){
return Main.lbtManager.getLbtBlockFromLocation(loc);
}
public static List<LinkedBeaconTeleporterBlock> getListFromTeleportId(String teleportId){
public static List<LinkedBeaconTeleporter> getListFromTeleportId(String teleportId){
return Main.lbtManager.getLbtBlocksFromTeleportId(teleportId);
}
}

View File

@ -1,10 +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;
@ -35,6 +42,50 @@ public class LinkedBeaconTeleporterItem extends LinkedBeaconTeleporter {
}
public LinkedBeaconTeleporterBlock place(BlockPlaceEvent e){
// Deny placing when 2 LinkedBeaconTeleporter's are already placed
if(this.linkedBeaconTeleporters.size() == 2){
e.getPlayer().sendMessage("§cMaximum-Amount §7(2) §cof §6LinkedBeaconTeleporters §cwith id §7"+ this.teleporterId +" §care already placed");
e.setCancelled(true);
return null;
}
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
);
// Notify Player when no other teleporter was found (yet?)
if(this.linkedBeaconTeleporters.size() == 2){
e.getPlayer().sendMessage("§6No other Teleporter found with with id §7"+ this.teleporterId);
}
// Notify Player when we have a full link
else if(this.linkedBeaconTeleporters.size() == 2){
e.getPlayer().sendMessage("§aConnected 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)");
}
return lbtBlock;
}
// Conversion methods
public static LinkedBeaconTeleporterItem getFromItemStack(ItemStack itemStack){
return Main.lbtManager.getLbtItemFromItemStack(itemStack);

View File

@ -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<LinkedBeaconTeleporterBlock>> 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;
@ -42,7 +42,7 @@ public class LinkedBeaconTeleporterManager {
ConfigurationSection teleportersData = Main.data.getConfigurationSection(teleporterId);
// New list for this id
ArrayList<LinkedBeaconTeleporterBlock> lbtBlocks = new ArrayList();
List<LinkedBeaconTeleporter> linkedBeaconTeleporters = new ArrayList();
for(String blockId : teleportersData.getKeys(false)) {
ConfigurationSection teleporterData = teleportersData.getConfigurationSection(blockId);
@ -67,61 +67,20 @@ public class LinkedBeaconTeleporterManager {
// Construct block
LinkedBeaconTeleporterBlock lbtBlock = new LinkedBeaconTeleporterBlock(teleporterId, block);
// Add block to id-list
lbtBlocks.add(lbtBlock);
// Write reference of list
lbtBlock.linkedBeaconTeleporters = linkedBeaconTeleporters;
// Save to loc-list
linkedBeaconTeleporterByLoc.put(serializedLoc, lbtBlock);
placedLBTByLoc.put(serializedLoc, lbtBlock);
}
// Save list to id-list
linkedBeaconTeleporterById.put(teleporterId, lbtBlocks);
placedLBTsById.put(teleporterId, linkedBeaconTeleporters);
}
Main.log.info("All done!");
}
public LinkedBeaconTeleporterBlock placeLbtItem(LinkedBeaconTeleporterItem lbtItem, Block block){
// Check if id already exists
List<LinkedBeaconTeleporterBlock> 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<LinkedBeaconTeleporterBlock> 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("-");
@ -196,10 +155,10 @@ public class LinkedBeaconTeleporterManager {
// Serialize
String serializedLoc = Function.serialiseBlockLocation(loc);
return linkedBeaconTeleporterByLoc.get(serializedLoc);
return placedLBTByLoc.get(serializedLoc);
}
public List<LinkedBeaconTeleporterBlock> getLbtBlocksFromTeleportId(String teleportId){
return linkedBeaconTeleporterById.get(teleportId);
public List<LinkedBeaconTeleporter> getLbtBlocksFromTeleportId(String teleportId){
return placedLBTsById.get(teleportId);
}
String constructRegex(String name, List<String> lore){

View File

@ -26,18 +26,7 @@ public class OnBlockBreak implements Listener {
// Ignore if not found
if(lbtBlock == null) return;
LinkedBeaconTeleporterItem lbtItem = Main.lbtManager.breakLbtBlock(lbtBlock);
// Dont drop anything
e.setDropItems(false);
e.setExpToDrop(0);
// Drop custom-item
if(e.getPlayer().getGameMode() != GameMode.CREATIVE)
lbtBlock.block().getLocation().getWorld().dropItemNaturally(
lbtBlock.block().getLocation(),
lbtItem.item()
);
LinkedBeaconTeleporterItem lbtItem = lbtBlock.break_(e);
}
}
}

View File

@ -25,8 +25,7 @@ public class OnBlockPlace implements Listener {
// Ignore if not found
if(lbtItem == null) return;
Block block = e.getBlock();
LinkedBeaconTeleporterBlock lbtBlock = Main.lbtManager.placeLbtItem(lbtItem, block);
LinkedBeaconTeleporterBlock lbtBlock = lbtItem.place(e);
}
}
}

View File

@ -4,7 +4,9 @@ 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.LinkedBeaconTeleporter;
import eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter.LinkedBeaconTeleporterBlock;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
@ -28,9 +30,18 @@ public class OnCustomPlayerMove implements CustomPlayerMoveEventListener {
Player p = e.player();
UUID uuid = p.getUniqueId();
// If player found in ignoreList, remove
if(playerBeaconLocation.containsKey(uuid))
playerBeaconLocation.remove(uuid);
// If player found in ignoreList, check if he stepped out of the Beacon-light
if(playerBeaconLocation.containsKey(uuid)) {
LinkedBeaconTeleporterBlock lbtBlock = playerBeaconLocation.get(uuid);
Location lbtBlockLoc = lbtBlock.block().getLocation();
Location pLoc = p.getLocation();
if(lbtBlockLoc.getBlockX() != pLoc.getBlockX() || lbtBlockLoc.getBlockZ() != pLoc.getBlockZ()){
playerBeaconLocation.remove(uuid);
}
else
// Player still on beacon, ignore
return;
}
// Check if player is on-top of a beacon
Block block = Function.searchForMaterial(
@ -54,20 +65,20 @@ public class OnCustomPlayerMove implements CustomPlayerMoveEventListener {
// Check if player should be ignored on this LinkedBeaconTeleporter
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
LinkedBeaconTeleporterBlock lbtBlockPartner = null;
for(LinkedBeaconTeleporterBlock lbtBlockTmp: lbtBlocks){
if(lbtBlockTmp != lbtBlocks){
lbtBlockPartner = lbtBlockTmp;
for(LinkedBeaconTeleporter lbtBlockTmp: lbtBlock.linkedBeaconTeleporters()){
// Upcast as linked-Teleporters are only blocks anyways
if((LinkedBeaconTeleporterBlock)lbtBlockTmp != lbtBlock){
lbtBlockPartner = (LinkedBeaconTeleporterBlock)lbtBlockTmp;
break;
}
}
if(lbtBlockPartner == null){
// No other partner
// Set player as ignored on this LinkedBeaconTeleporter (so he wont trigger the checks again)
playerBeaconLocation.put(uuid, lbtBlock);
}
else{
// Set player as ignored on target-LinkedBeaconTeleporter (so he wont trigger the teleport again)
@ -75,7 +86,7 @@ public class OnCustomPlayerMove implements CustomPlayerMoveEventListener {
// Teleport
e.player().teleport(
lbtBlockPartner.block().getLocation().add(0, 1, 0)
lbtBlockPartner.block().getLocation().add(0.5, 1, 0.5)
);
}
}