Compare commits
21 Commits
6c2e06afaf
...
1.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c1dbe281a6 | ||
|
|
8485e34756 | ||
|
|
7e6601d56d | ||
|
|
982190b730 | ||
|
|
4626646292 | ||
|
|
e82de277ef | ||
|
|
28658329a8 | ||
|
|
ac810349ac | ||
|
|
2a713d269e | ||
|
|
b20e0a03fd | ||
|
|
5a1c809d89 | ||
|
|
ef9643c2ae | ||
|
|
c732aa31b1 | ||
|
|
51c9ee0716 | ||
|
|
e10eaf9b66 | ||
|
|
686ad312c5 | ||
|
|
973897cc3e | ||
|
|
1eb472b576 | ||
|
|
c41ab95baf | ||
|
|
5e22456f61 | ||
|
|
2bda948c8f |
@@ -35,19 +35,20 @@ public class Main extends JavaPlugin {
|
||||
pluginManager.registerEvents(new OnBlockPlace(), this);
|
||||
pluginManager.registerEvents(new OnCraftItemEvent(), this);
|
||||
|
||||
CustomPlayerMoveEventHandler cpmHandler = new CustomPlayerMoveEventHandler(this, true);
|
||||
cpmHandler.registerListener(new OnCustomPlayerMove());
|
||||
|
||||
loadConfigs();
|
||||
|
||||
CustomPlayerMoveEventHandler cpmHandler = new CustomPlayerMoveEventHandler(this, config.getInt("locationCheck.interval"), true);
|
||||
cpmHandler.registerListener(new OnCustomPlayerMove());
|
||||
|
||||
LinkedBeaconTeleporterManager.init();
|
||||
|
||||
initAutoSave();
|
||||
|
||||
log.info("Plugin activated");
|
||||
}
|
||||
|
||||
public void onDisable() {
|
||||
|
||||
LinkedBeaconTeleporterManager.saveData();
|
||||
saveData();
|
||||
|
||||
log.info("Plugin deactivated");
|
||||
}
|
||||
@@ -71,7 +72,15 @@ public class Main extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveData() {
|
||||
public static void saveData(){
|
||||
boolean lbtDataChanged = LinkedBeaconTeleporterManager.writeData();
|
||||
|
||||
// Only run save, when data changed
|
||||
if(lbtDataChanged)
|
||||
saveFiles();
|
||||
}
|
||||
|
||||
public static void saveFiles() {
|
||||
// data.yml
|
||||
try{
|
||||
File dataFile = new File("plugins/"+ plugin.getName() +"/data.yml");
|
||||
@@ -81,4 +90,12 @@ public class Main extends JavaPlugin {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
static void initAutoSave(){
|
||||
int autosaveInterval = config.getInt("config.interval")*20;
|
||||
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> {
|
||||
saveData();
|
||||
|
||||
}, autosaveInterval, autosaveInterval);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,16 +17,16 @@ public class CustomPlayerMoveEventHandler {
|
||||
HashMap<UUID, Location> oldPlayerLoc = new HashMap<>();
|
||||
|
||||
boolean locationChangeOnBlockChange;
|
||||
public CustomPlayerMoveEventHandler(Plugin plugin, boolean locationChangeOnBlockChange){
|
||||
public CustomPlayerMoveEventHandler(Plugin plugin, int interval, boolean locationChangeOnBlockChange){
|
||||
this.locationChangeOnBlockChange = locationChangeOnBlockChange;
|
||||
|
||||
startRunnable(plugin);
|
||||
startRunnable(plugin, interval);
|
||||
}
|
||||
public CustomPlayerMoveEventHandler(Plugin plugin){
|
||||
this(plugin, false);
|
||||
this(plugin, 5, false);
|
||||
}
|
||||
|
||||
void startRunnable(Plugin plugin){
|
||||
void startRunnable(Plugin plugin, int interval){
|
||||
|
||||
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> {
|
||||
// Dont do anything without listeners
|
||||
@@ -52,7 +52,9 @@ public class CustomPlayerMoveEventHandler {
|
||||
if(distance > 0)
|
||||
locationChangeListeners(e);
|
||||
}
|
||||
else
|
||||
else if(loc.getBlockX() != oldLoc.getBlockX() ||
|
||||
loc.getBlockY() != oldLoc.getBlockY() ||
|
||||
loc.getBlockZ() != oldLoc.getBlockZ())
|
||||
locationChangeListeners(e);
|
||||
}
|
||||
|
||||
@@ -62,7 +64,7 @@ public class CustomPlayerMoveEventHandler {
|
||||
oldPlayerLoc.put(uuid, loc);
|
||||
}
|
||||
|
||||
}, 20, 10);
|
||||
}, interval, interval);
|
||||
}
|
||||
|
||||
boolean blockLocationChanged(Location loc, Location oldLoc){
|
||||
|
||||
@@ -11,6 +11,8 @@ import java.util.List;
|
||||
* Describes a placed-beacon-teleporter in the world
|
||||
*/
|
||||
public abstract class LinkedBeaconTeleporter {
|
||||
static String id_alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||
static int id_length = 10;
|
||||
|
||||
// Persistent id for linked-beacons
|
||||
public String teleporterId;
|
||||
@@ -19,7 +21,7 @@ public abstract class LinkedBeaconTeleporter {
|
||||
protected List<LinkedBeaconTeleporter> linkedBeaconTeleporters = new ArrayList<>();
|
||||
|
||||
LinkedBeaconTeleporter(){
|
||||
this.teleporterId = Function.randomString("abcdefghijklmnopqrstuvwxyz0123456789", 10);
|
||||
this.teleporterId = Function.randomString(id_alphabet, id_length);
|
||||
|
||||
linkedBeaconTeleporters = new ArrayList<>();
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ public class LinkedBeaconTeleporterBlock extends LinkedBeaconTeleporter {
|
||||
e.setExpToDrop(0);
|
||||
|
||||
// Notify Player when link was destroyed
|
||||
if(this.linkedBeaconTeleporters.size()-1 != 2){
|
||||
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()
|
||||
@@ -42,7 +42,13 @@ public class LinkedBeaconTeleporterBlock extends LinkedBeaconTeleporter {
|
||||
|
||||
// Remove from list
|
||||
this.linkedBeaconTeleporters.remove(this);
|
||||
// TODO: Remove from placedLBTsById when empty?
|
||||
|
||||
if(this.linkedBeaconTeleporters.size() == 0){
|
||||
// List empty, delete from placedLBTsById
|
||||
LinkedBeaconTeleporterManager.placedLBTsById.remove(
|
||||
this.teleporterId
|
||||
);
|
||||
}
|
||||
|
||||
// Remove from loc
|
||||
LinkedBeaconTeleporterManager.placedLBTByLoc.remove(
|
||||
|
||||
@@ -56,6 +56,10 @@ public class LinkedBeaconTeleporterItem extends LinkedBeaconTeleporter {
|
||||
|
||||
// Block from item
|
||||
LinkedBeaconTeleporterBlock lbtBlock = new LinkedBeaconTeleporterBlock(this.teleporterId(), block);
|
||||
// Both objects have different lists (as both are new), overwrite block-list with item-list
|
||||
// FIXME: This seems like a bad design.. maybe only create the list when necessary? (e.g. keep null if no linked objects found)
|
||||
lbtBlock.linkedBeaconTeleporters = this.linkedBeaconTeleporters;
|
||||
|
||||
// Add to list
|
||||
this.linkedBeaconTeleporters.add(lbtBlock);
|
||||
|
||||
|
||||
@@ -34,6 +34,10 @@ public class LinkedBeaconTeleporterManager {
|
||||
Main.config.getStringList("item.lore")
|
||||
);
|
||||
|
||||
// Load settings
|
||||
LinkedBeaconTeleporter.id_alphabet = Main.config.getString("id.alphabet");
|
||||
LinkedBeaconTeleporter.id_length = Main.config.getInt("id.length");
|
||||
|
||||
// Construct & add crafting-receipes
|
||||
constructAndAddRecipes();
|
||||
|
||||
@@ -68,6 +72,8 @@ public class LinkedBeaconTeleporterManager {
|
||||
// Write reference of list
|
||||
lbtBlock.linkedBeaconTeleporters = linkedBeaconTeleporters;
|
||||
|
||||
// Write block to list
|
||||
linkedBeaconTeleporters.add(lbtBlock);
|
||||
// Save to loc-list
|
||||
placedLBTByLoc.put(serializedLoc, lbtBlock);
|
||||
}
|
||||
@@ -76,11 +82,21 @@ public class LinkedBeaconTeleporterManager {
|
||||
placedLBTsById.put(teleporterId, linkedBeaconTeleporters);
|
||||
}
|
||||
|
||||
Main.saveData();
|
||||
|
||||
Main.log.info("All done!");
|
||||
}
|
||||
|
||||
public static void saveData(){
|
||||
Main.log.info("Saving data..");
|
||||
static int oldDataHashCode;
|
||||
public static boolean writeData(){
|
||||
// Check if data changed
|
||||
int dataHashCode = placedLBTsById.hashCode();
|
||||
if(oldDataHashCode != dataHashCode){
|
||||
// Changed
|
||||
oldDataHashCode = dataHashCode;
|
||||
}
|
||||
else // Did not change
|
||||
return false;
|
||||
|
||||
Main.data = new YamlConfiguration();
|
||||
for(String teleporterId : placedLBTsById.keySet()){
|
||||
@@ -98,10 +114,7 @@ public class LinkedBeaconTeleporterManager {
|
||||
|
||||
Main.data.set(teleporterId, dataList);
|
||||
}
|
||||
|
||||
Main.saveData();
|
||||
|
||||
Main.log.info("done");
|
||||
return true;
|
||||
}
|
||||
|
||||
static void constructAndAddRecipes(){
|
||||
|
||||
@@ -69,32 +69,50 @@ public class OnCustomPlayerMove implements CustomPlayerMoveEventListener {
|
||||
// Get first partner thats not our current-block
|
||||
LinkedBeaconTeleporterBlock lbtBlockPartner = null;
|
||||
for(LinkedBeaconTeleporter lbtBlockTmp: lbtBlock.linkedBeaconTeleporters()){
|
||||
// Upcast as linked-Teleporters are only blocks anyways
|
||||
if((LinkedBeaconTeleporterBlock)lbtBlockTmp != lbtBlock){
|
||||
if(lbtBlockTmp != lbtBlock){
|
||||
lbtBlockPartner = (LinkedBeaconTeleporterBlock)lbtBlockTmp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(lbtBlockPartner == null){
|
||||
// No other partner
|
||||
p.sendMessage("§cNo linked beacon found with id §7"+ lbtBlock.teleporterId());
|
||||
|
||||
// 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)
|
||||
playerBeaconLocation.put(uuid, lbtBlockPartner);
|
||||
// TODO: Check if beacon is active
|
||||
|
||||
// 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
|
||||
Material.AIR,
|
||||
Function.transparentMaterials
|
||||
);
|
||||
|
||||
if(safeBlock == null){
|
||||
// No safe location found
|
||||
p.sendMessage("§cNo safe-location found at target teleporter");
|
||||
playerBeaconLocation.put(uuid, lbtBlock);
|
||||
return;
|
||||
}
|
||||
// TODO: Check if there is enough space for a player
|
||||
|
||||
// Set player as ignored on target-LinkedBeaconTeleporter (so he wont trigger the teleport again)
|
||||
playerBeaconLocation.put(uuid, lbtBlockPartner);
|
||||
|
||||
// Teleport
|
||||
Location loc = safeBlock.getLocation();
|
||||
// Middle of block
|
||||
loc.add(0.5, 0, 0.5);
|
||||
// Preserve pitch and yaw from player
|
||||
loc.setPitch(p.getLocation().getPitch());
|
||||
loc.setYaw(p.getLocation().getYaw());
|
||||
|
||||
e.player().teleport(
|
||||
safeBlock.getLocation().add(0.5, 0, 0.5)
|
||||
loc
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,28 @@
|
||||
|
||||
item: # TODO
|
||||
item:
|
||||
# Name & Lore for linked-beacon-blocks
|
||||
# - Has to include %id% somewhere for identification!
|
||||
name: '§9BeaconTeleporter'
|
||||
lore:
|
||||
- '§8§o%id%'
|
||||
- '§8§o%id%'
|
||||
|
||||
beacon:
|
||||
# Checks that have to succeed for a teleporter to be active (source and target)
|
||||
checks: # TODO
|
||||
active: true
|
||||
|
||||
id:
|
||||
# Alphabet to use for random-generation
|
||||
alphabet: 'abcdefghijklmnopqrstuvwxyz0123456789'
|
||||
# Length of id
|
||||
# If its too short, ids will "collide" more often (e.g. 2 players generating same linked-teleporters independently), but maybe thats a gameplay-feature for you :)
|
||||
length: 10
|
||||
|
||||
locationCheck:
|
||||
# Interval [in ticks] to check if a player is on a beacon
|
||||
# 5 (= 4 times/s) is usually responsive enough for most interactions
|
||||
interval: 5
|
||||
|
||||
autosave:
|
||||
# Interval [in seconds] for autosave
|
||||
interval: 600
|
||||
Reference in New Issue
Block a user