Compare commits

..

No commits in common. "8485e34756932d470f1c08f45c72f59479d83485" and "51c9ee0716877d95d730e0ce29b8d450202618fb" have entirely different histories.

7 changed files with 21 additions and 81 deletions

View File

@ -35,20 +35,19 @@ public class Main extends JavaPlugin {
pluginManager.registerEvents(new OnBlockPlace(), this); pluginManager.registerEvents(new OnBlockPlace(), this);
pluginManager.registerEvents(new OnCraftItemEvent(), this); pluginManager.registerEvents(new OnCraftItemEvent(), this);
loadConfigs(); CustomPlayerMoveEventHandler cpmHandler = new CustomPlayerMoveEventHandler(this, true);
CustomPlayerMoveEventHandler cpmHandler = new CustomPlayerMoveEventHandler(this, config.getInt("locationCheck.interval"), true);
cpmHandler.registerListener(new OnCustomPlayerMove()); cpmHandler.registerListener(new OnCustomPlayerMove());
LinkedBeaconTeleporterManager.init(); loadConfigs();
initAutoSave(); LinkedBeaconTeleporterManager.init();
log.info("Plugin activated"); log.info("Plugin activated");
} }
public void onDisable() { public void onDisable() {
saveData();
LinkedBeaconTeleporterManager.saveData();
log.info("Plugin deactivated"); log.info("Plugin deactivated");
} }
@ -72,15 +71,7 @@ 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 // data.yml
try{ try{
File dataFile = new File("plugins/"+ plugin.getName() +"/data.yml"); File dataFile = new File("plugins/"+ plugin.getName() +"/data.yml");
@ -90,12 +81,4 @@ public class Main extends JavaPlugin {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
static void initAutoSave(){
int autosaveInterval = config.getInt("config.interval")*20;
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> {
saveData();
}, autosaveInterval, autosaveInterval);
}
} }

View File

@ -17,16 +17,16 @@ public class CustomPlayerMoveEventHandler {
HashMap<UUID, Location> oldPlayerLoc = new HashMap<>(); HashMap<UUID, Location> oldPlayerLoc = new HashMap<>();
boolean locationChangeOnBlockChange; boolean locationChangeOnBlockChange;
public CustomPlayerMoveEventHandler(Plugin plugin, int interval, boolean locationChangeOnBlockChange){ public CustomPlayerMoveEventHandler(Plugin plugin, boolean locationChangeOnBlockChange){
this.locationChangeOnBlockChange = locationChangeOnBlockChange; this.locationChangeOnBlockChange = locationChangeOnBlockChange;
startRunnable(plugin, interval); startRunnable(plugin);
} }
public CustomPlayerMoveEventHandler(Plugin plugin){ public CustomPlayerMoveEventHandler(Plugin plugin){
this(plugin, 5, false); this(plugin, false);
} }
void startRunnable(Plugin plugin, int interval){ void startRunnable(Plugin plugin){
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> { Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> {
// Dont do anything without listeners // Dont do anything without listeners
@ -64,7 +64,7 @@ public class CustomPlayerMoveEventHandler {
oldPlayerLoc.put(uuid, loc); oldPlayerLoc.put(uuid, loc);
} }
}, interval, interval); }, 20, 10);
} }
boolean blockLocationChanged(Location loc, Location oldLoc){ boolean blockLocationChanged(Location loc, Location oldLoc){

View File

@ -11,8 +11,6 @@ import java.util.List;
* Describes a placed-beacon-teleporter in the world * Describes a placed-beacon-teleporter in the world
*/ */
public abstract class LinkedBeaconTeleporter { public abstract class LinkedBeaconTeleporter {
static String id_alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
static int id_length = 10;
// Persistent id for linked-beacons // Persistent id for linked-beacons
public String teleporterId; public String teleporterId;
@ -21,7 +19,7 @@ public abstract class LinkedBeaconTeleporter {
protected List<LinkedBeaconTeleporter> linkedBeaconTeleporters = new ArrayList<>(); protected List<LinkedBeaconTeleporter> linkedBeaconTeleporters = new ArrayList<>();
LinkedBeaconTeleporter(){ LinkedBeaconTeleporter(){
this.teleporterId = Function.randomString(id_alphabet, id_length); this.teleporterId = Function.randomString("abcdefghijklmnopqrstuvwxyz0123456789", 10);
linkedBeaconTeleporters = new ArrayList<>(); linkedBeaconTeleporters = new ArrayList<>();
} }

View File

@ -42,13 +42,7 @@ public class LinkedBeaconTeleporterBlock extends LinkedBeaconTeleporter {
// Remove from list // Remove from list
this.linkedBeaconTeleporters.remove(this); 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 // Remove from loc
LinkedBeaconTeleporterManager.placedLBTByLoc.remove( LinkedBeaconTeleporterManager.placedLBTByLoc.remove(

View File

@ -34,10 +34,6 @@ public class LinkedBeaconTeleporterManager {
Main.config.getStringList("item.lore") 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 // Construct & add crafting-receipes
constructAndAddRecipes(); constructAndAddRecipes();
@ -72,8 +68,6 @@ public class LinkedBeaconTeleporterManager {
// Write reference of list // Write reference of list
lbtBlock.linkedBeaconTeleporters = linkedBeaconTeleporters; lbtBlock.linkedBeaconTeleporters = linkedBeaconTeleporters;
// Write block to list
linkedBeaconTeleporters.add(lbtBlock);
// Save to loc-list // Save to loc-list
placedLBTByLoc.put(serializedLoc, lbtBlock); placedLBTByLoc.put(serializedLoc, lbtBlock);
} }
@ -82,21 +76,11 @@ public class LinkedBeaconTeleporterManager {
placedLBTsById.put(teleporterId, linkedBeaconTeleporters); placedLBTsById.put(teleporterId, linkedBeaconTeleporters);
} }
Main.saveData();
Main.log.info("All done!"); Main.log.info("All done!");
} }
static int oldDataHashCode; public static void saveData(){
public static boolean writeData(){ Main.log.info("Saving data..");
// Check if data changed
int dataHashCode = placedLBTsById.hashCode();
if(oldDataHashCode != dataHashCode){
// Changed
oldDataHashCode = dataHashCode;
}
else // Did not change
return false;
Main.data = new YamlConfiguration(); Main.data = new YamlConfiguration();
for(String teleporterId : placedLBTsById.keySet()){ for(String teleporterId : placedLBTsById.keySet()){
@ -114,7 +98,10 @@ public class LinkedBeaconTeleporterManager {
Main.data.set(teleporterId, dataList); Main.data.set(teleporterId, dataList);
} }
return true;
Main.saveData();
Main.log.info("done");
} }
static void constructAndAddRecipes(){ static void constructAndAddRecipes(){

View File

@ -95,7 +95,6 @@ public class OnCustomPlayerMove implements CustomPlayerMoveEventListener {
if(safeBlock == null){ if(safeBlock == null){
// No safe location found // No safe location found
p.sendMessage("§cNo safe-location found at target teleporter"); p.sendMessage("§cNo safe-location found at target teleporter");
playerBeaconLocation.put(uuid, lbtBlock);
return; return;
} }
// TODO: Check if there is enough space for a player // TODO: Check if there is enough space for a player

View File

@ -1,28 +1,7 @@
item: item: # TODO
# Name & Lore for linked-beacon-blocks # Name & Lore for linked-beacon-blocks
# - Has to include %id% somewhere for identification! # - Has to include %id% somewhere for identification!
name: '§9BeaconTeleporter' name: '§9BeaconTeleporter'
lore: 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