Gens only tick when loaded
This commit is contained in:
@@ -5,6 +5,7 @@ import io.github.itzispyder.pdk.utils.SchedulerUtils;
|
||||
import io.github.itzispyder.pdk.utils.misc.JsonSerializable;
|
||||
import io.github.thetrouper.sssbliss.cmds.ChatClickCallback;
|
||||
import io.github.thetrouper.sssbliss.cmds.SSSBlissCommand;
|
||||
import io.github.thetrouper.sssbliss.data.DataSerializer;
|
||||
import io.github.thetrouper.sssbliss.data.GenStorage;
|
||||
import io.github.thetrouper.sssbliss.data.config.LanguageFile;
|
||||
import io.github.thetrouper.sssbliss.data.config.MainConfig;
|
||||
@@ -20,6 +21,7 @@ import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public final class SSSBliss extends JavaPlugin {
|
||||
@@ -27,13 +29,14 @@ public final class SSSBliss extends JavaPlugin {
|
||||
|
||||
private static SSSBliss instance;
|
||||
private static final File cfgfile = new File("plugins/SSSBliss/main-config.json");
|
||||
private static final File storageFile = new File("plugins/SSSBliss/storage.json");
|
||||
public static GenStorage storage = JsonSerializable.load(storageFile, GenStorage.class, new GenStorage());
|
||||
public static final File storageFile = new File("plugins/SSSBliss/storage.db");
|
||||
public static MainConfig mainConfig = JsonSerializable.load(cfgfile, MainConfig.class, new MainConfig());
|
||||
public static LanguageFile language;
|
||||
public static final PluginManager manager = Bukkit.getPluginManager();
|
||||
public static final Logger log = Bukkit.getLogger();
|
||||
|
||||
public static GenStorage storage = new GenStorage();
|
||||
|
||||
/**
|
||||
* Plugin startup logic
|
||||
*/
|
||||
@@ -71,21 +74,21 @@ public final class SSSBliss extends JavaPlugin {
|
||||
new ChatClickCallback().register();
|
||||
|
||||
// Events
|
||||
new GemUseListener().register();
|
||||
new ProjectileHitListener().register();
|
||||
new DeathListener().register();
|
||||
new UpgradeListener().register();
|
||||
new TradeListener().register();
|
||||
new JoinListener().register();
|
||||
new GemMoveListener().register();
|
||||
new TrollListener().register();
|
||||
// new GemUseListener().register();
|
||||
// new ProjectileHitListener().register();
|
||||
// new DeathListener().register();
|
||||
// new UpgradeListener().register();
|
||||
//new TradeListener().register();
|
||||
//new JoinListener().register();
|
||||
//new GemMoveListener().register();
|
||||
//new TrollListener().register();
|
||||
new GenBreakEvent().register();
|
||||
new GenPlaceEvent().register();
|
||||
|
||||
// Scheduled timers
|
||||
SchedulerUtils.repeat(20, GemPassiveEffects::applyPassiveEffects);
|
||||
SchedulerUtils.repeat(20, MakeInvulnerable::updateInvul);
|
||||
SchedulerUtils.repeat(100, GenFunctions::tickGens);
|
||||
//SchedulerUtils.repeat(20, GemPassiveEffects::applyPassiveEffects);
|
||||
//SchedulerUtils.repeat(20, MakeInvulnerable::updateInvul);
|
||||
SchedulerUtils.repeat(200, GenFunctions::tickGens);
|
||||
|
||||
log.info("""
|
||||
\n,ggggggggggg, \s
|
||||
@@ -106,9 +109,13 @@ public final class SSSBliss extends JavaPlugin {
|
||||
|
||||
// Init
|
||||
mainConfig = JsonSerializable.load(cfgfile, MainConfig.class, new MainConfig());
|
||||
|
||||
//storage = Serializable.load(storageFile, GenStorage.class, new GenStorage());
|
||||
loadStorage();
|
||||
// Save
|
||||
mainConfig.save();
|
||||
//storage.save(storageFile);
|
||||
saveStorage();
|
||||
|
||||
|
||||
log.info("Loading Dictionary (" + SSSBliss.mainConfig.plugin.lang + ")...");
|
||||
|
||||
@@ -123,6 +130,7 @@ public final class SSSBliss extends JavaPlugin {
|
||||
@Override
|
||||
public void onDisable() {
|
||||
// Plugin shutdown logic
|
||||
saveStorage();
|
||||
log.info("SSSBliss has disabled! (" + getDescription().getVersion() + ") Your server is now no longer protected!");
|
||||
}
|
||||
|
||||
@@ -160,4 +168,25 @@ public final class SSSBliss extends JavaPlugin {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static void saveStorage() {
|
||||
try {
|
||||
DataSerializer.serialize(storage, storageFile);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public static void loadStorage() {
|
||||
try {
|
||||
if (storageFile.exists()) {
|
||||
// If storage file exists, deserialize it
|
||||
storage = (GenStorage) DataSerializer.deserialize(storageFile);
|
||||
} else {
|
||||
// If storage file doesn't exist, create a new instance
|
||||
storage = new GenStorage();
|
||||
DataSerializer.serialize(storage, storageFile);
|
||||
}
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,7 +191,7 @@ public class SSSBlissCommand implements CustomCommand {
|
||||
|
||||
@Override
|
||||
public void dispatchCompletions(CompletionBuilder b) {
|
||||
b.then(b.arg("get").then(b.arg("iron","gold")));
|
||||
b.then(b.arg("get").then(b.arg("iron","gold","diamond","kelp","coal")));
|
||||
b.then(b.arg("reload"));
|
||||
b.then(b.arg("gems").then(
|
||||
b.arg("give")
|
||||
@@ -201,9 +201,9 @@ public class SSSBlissCommand implements CustomCommand {
|
||||
b.arg("encrypt","lang","toggle")));
|
||||
}
|
||||
|
||||
private final GenInfo ironGen = new GenInfo(Material.IRON_BLOCK.name(),Material.IRON_ORE.name());
|
||||
private final GenInfo diamondGen = new GenInfo(Material.DIAMOND_BLOCK.name(),Material.DIAMOND_ORE.name());
|
||||
private final GenInfo goldGen = new GenInfo(Material.GOLD_BLOCK.name(),Material.GOLD_ORE.name());
|
||||
private final GenInfo coalGen = new GenInfo(Material.COAL_BLOCK.name(),Material.COAL_ORE.name());
|
||||
private final GenInfo kelpGen = new GenInfo(Material.DRIED_KELP_BLOCK.name(),Material.DRIED_KELP_BLOCK.name());
|
||||
private final GenInfo ironGen = new GenInfo(Material.IRON_ORE.name(),Material.IRON_INGOT.name());
|
||||
private final GenInfo diamondGen = new GenInfo(Material.DIAMOND_ORE.name(),Material.DIAMOND.name());
|
||||
private final GenInfo goldGen = new GenInfo(Material.GOLD_ORE.name(),Material.GOLD_INGOT.name());
|
||||
private final GenInfo coalGen = new GenInfo(Material.COAL_ORE.name(),Material.COAL.name());
|
||||
private final GenInfo kelpGen = new GenInfo(Material.DRIED_KELP_BLOCK.name(),Material.KELP.name());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package io.github.thetrouper.sssbliss.data;
|
||||
|
||||
import io.github.itzispyder.pdk.utils.FileValidationUtils;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public interface CustomSerializable<T> {
|
||||
File getFile();
|
||||
|
||||
default String serialize() {
|
||||
try {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
|
||||
objectOutputStream.writeObject(this);
|
||||
objectOutputStream.close();
|
||||
return byteArrayOutputStream.toString();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
default T deserialize(String serialized) {
|
||||
try {
|
||||
byte[] bytes = serialized.getBytes();
|
||||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
|
||||
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
|
||||
Object obj = objectInputStream.readObject();
|
||||
objectInputStream.close();
|
||||
return (T) obj;
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
default void save() {
|
||||
String serialized = serialize();
|
||||
File file = getFile();
|
||||
if (FileValidationUtils.validate(file)) {
|
||||
try {
|
||||
FileWriter fw = new FileWriter(file);
|
||||
BufferedWriter bw = new BufferedWriter(fw);
|
||||
bw.write(serialized);
|
||||
bw.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static <T extends CustomSerializable<T>> T load(File file, T fallback) {
|
||||
if (FileValidationUtils.validate(file)) {
|
||||
try {
|
||||
FileReader fr = new FileReader(file);
|
||||
BufferedReader br = new BufferedReader(fr);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
br.close();
|
||||
String serialized = sb.toString();
|
||||
return fallback.deserialize(serialized);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
static <T extends CustomSerializable<T>> T load(String path, T fallback) {
|
||||
return load(new File(path), fallback);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.github.thetrouper.sssbliss.data;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public interface DataSerializer {
|
||||
static void serialize(Object data, File file) throws IOException {
|
||||
try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file))) {
|
||||
outputStream.writeObject(data);
|
||||
}
|
||||
}
|
||||
|
||||
static Object deserialize(File file) throws IOException, ClassNotFoundException {
|
||||
try (ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file))) {
|
||||
return inputStream.readObject();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,9 @@ import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
public record GenInfo(String block, String drop) {
|
||||
import java.io.Serializable;
|
||||
|
||||
public record GenInfo(String block, String drop) implements Serializable {
|
||||
|
||||
private static final NamespacedKey KEY = new NamespacedKey(Global.instance.getPlugin(), "genInfo");
|
||||
|
||||
@@ -26,7 +28,7 @@ public record GenInfo(String block, String drop) {
|
||||
public ItemStack getItem() {
|
||||
ItemStack genItem = new ItemBuilder()
|
||||
.material(getBlock(block))
|
||||
.name(Text.color("&a%s&7 Generator".formatted(Text.cleanName(getBlock(block).name()))))
|
||||
.name(Text.color("&a%s&7 Generator".formatted(Text.cleanName(getBlock(drop).name()))))
|
||||
.lore(Text.color("&7Drops 1x %s".formatted(Text.cleanName(getBlock(drop).name()))))
|
||||
.build();
|
||||
updateItem(genItem);
|
||||
|
||||
@@ -1,19 +1,9 @@
|
||||
package io.github.thetrouper.sssbliss.data;
|
||||
|
||||
import io.github.itzispyder.pdk.utils.misc.JsonSerializable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GenStorage implements JsonSerializable<GenStorage> {
|
||||
|
||||
@Override
|
||||
public File getFile() {
|
||||
File file = new File("plugins/SSSBliss/storage.json");
|
||||
file.getParentFile().mkdirs();
|
||||
return file;
|
||||
}
|
||||
|
||||
public class GenStorage implements Serializable {
|
||||
public List<PlacedGen> gens = new ArrayList<>();
|
||||
}
|
||||
|
||||
@@ -5,7 +5,9 @@ import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public record PlacedGen(String world, int x, int y, int z, GenInfo info) {
|
||||
import java.io.Serializable;
|
||||
|
||||
public record PlacedGen(String world, int x, int y, int z, GenInfo info) implements Serializable {
|
||||
|
||||
public Location getLocation() {
|
||||
return new Location(getWorld(), x, y, z);
|
||||
@@ -18,6 +20,6 @@ public record PlacedGen(String world, int x, int y, int z, GenInfo info) {
|
||||
public void drop() {
|
||||
var loc = getLocation();
|
||||
var item = new ItemStack(GenInfo.getBlock(info.drop()));
|
||||
loc.getWorld().dropItem(loc.clone().add(0, 1, 0), item);
|
||||
loc.getWorld().dropItem(loc.clone().add(0.5, 1, 0.5), item);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ public class GenBreakEvent implements CustomListener {
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onBreak(BlockBreakEvent e) {
|
||||
e.getPlayer().sendMessage("broke block");
|
||||
//e.getPlayer().sendMessage("broke block");
|
||||
if (e.isCancelled())
|
||||
return;
|
||||
|
||||
@@ -24,7 +24,7 @@ public class GenBreakEvent implements CustomListener {
|
||||
e.setCancelled(true);
|
||||
b.setType(Material.AIR);
|
||||
SSSBliss.storage.gens.remove(gen);
|
||||
SSSBliss.storage.save();
|
||||
SSSBliss.saveStorage();
|
||||
|
||||
ItemStack drop = gen.info().getItem();
|
||||
loc.getWorld().dropItem(loc,drop);
|
||||
|
||||
@@ -17,7 +17,7 @@ public class GenPlaceEvent implements CustomListener {
|
||||
|
||||
@EventHandler
|
||||
public void placeGen(BlockPlaceEvent e) {
|
||||
e.getPlayer().sendMessage("placed block");
|
||||
//e.getPlayer().sendMessage("placed block");
|
||||
Player p = e.getPlayer();
|
||||
Block b = e.getBlock();
|
||||
ItemStack i = e.getItemInHand();
|
||||
@@ -28,7 +28,7 @@ public class GenPlaceEvent implements CustomListener {
|
||||
b.setType(GenInfo.getBlock(gen.block()));
|
||||
PlacedGen newGen = new PlacedGen(b.getWorld().getName(),b.getX(),b.getY(),b.getZ(),gen);
|
||||
SSSBliss.storage.gens.add(newGen);
|
||||
SSSBliss.storage.save();
|
||||
SSSBliss.saveStorage();
|
||||
p.sendMessage(Text.prefix("You have placed a &a%s&7 gen".formatted(Text.cleanName(GenInfo.getBlock(gen.block()).name()))));
|
||||
DisplayUtils.vortex(b.getLocation(),0,DisplayUtils.DUST_PARTICLE_FACTORY.apply(Color.AQUA,0.5F),0.1,0.1,2);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.util.function.Consumer;
|
||||
public class GenFunctions {
|
||||
|
||||
public static void tickGens() {
|
||||
if (SSSBliss.storage.gens == null || SSSBliss.storage.gens.isEmpty()) return;
|
||||
accept(GenFunctions::genAction);
|
||||
}
|
||||
|
||||
@@ -16,6 +17,7 @@ public class GenFunctions {
|
||||
}
|
||||
|
||||
public static void genAction(PlacedGen gen) {
|
||||
if (!gen.getLocation().isChunkLoaded()) return;
|
||||
gen.drop();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user