Gens only tick when loaded

This commit is contained in:
TheTrouper
2024-04-28 15:19:27 -05:00
parent aca5c75cbe
commit 1fc79247a5
10 changed files with 159 additions and 41 deletions

View File

@@ -5,6 +5,7 @@ import io.github.itzispyder.pdk.utils.SchedulerUtils;
import io.github.itzispyder.pdk.utils.misc.JsonSerializable; import io.github.itzispyder.pdk.utils.misc.JsonSerializable;
import io.github.thetrouper.sssbliss.cmds.ChatClickCallback; import io.github.thetrouper.sssbliss.cmds.ChatClickCallback;
import io.github.thetrouper.sssbliss.cmds.SSSBlissCommand; 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.GenStorage;
import io.github.thetrouper.sssbliss.data.config.LanguageFile; import io.github.thetrouper.sssbliss.data.config.LanguageFile;
import io.github.thetrouper.sssbliss.data.config.MainConfig; import io.github.thetrouper.sssbliss.data.config.MainConfig;
@@ -20,6 +21,7 @@ import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.logging.Logger; import java.util.logging.Logger;
public final class SSSBliss extends JavaPlugin { public final class SSSBliss extends JavaPlugin {
@@ -27,13 +29,14 @@ public final class SSSBliss extends JavaPlugin {
private static SSSBliss instance; private static SSSBliss instance;
private static final File cfgfile = new File("plugins/SSSBliss/main-config.json"); 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 final File storageFile = new File("plugins/SSSBliss/storage.db");
public static GenStorage storage = JsonSerializable.load(storageFile, GenStorage.class, new GenStorage());
public static MainConfig mainConfig = JsonSerializable.load(cfgfile, MainConfig.class, new MainConfig()); public static MainConfig mainConfig = JsonSerializable.load(cfgfile, MainConfig.class, new MainConfig());
public static LanguageFile language; public static LanguageFile language;
public static final PluginManager manager = Bukkit.getPluginManager(); public static final PluginManager manager = Bukkit.getPluginManager();
public static final Logger log = Bukkit.getLogger(); public static final Logger log = Bukkit.getLogger();
public static GenStorage storage = new GenStorage();
/** /**
* Plugin startup logic * Plugin startup logic
*/ */
@@ -71,21 +74,21 @@ public final class SSSBliss extends JavaPlugin {
new ChatClickCallback().register(); new ChatClickCallback().register();
// Events // Events
new GemUseListener().register(); // new GemUseListener().register();
new ProjectileHitListener().register(); // new ProjectileHitListener().register();
new DeathListener().register(); // new DeathListener().register();
new UpgradeListener().register(); // new UpgradeListener().register();
new TradeListener().register(); //new TradeListener().register();
new JoinListener().register(); //new JoinListener().register();
new GemMoveListener().register(); //new GemMoveListener().register();
new TrollListener().register(); //new TrollListener().register();
new GenBreakEvent().register(); new GenBreakEvent().register();
new GenPlaceEvent().register(); new GenPlaceEvent().register();
// Scheduled timers // Scheduled timers
SchedulerUtils.repeat(20, GemPassiveEffects::applyPassiveEffects); //SchedulerUtils.repeat(20, GemPassiveEffects::applyPassiveEffects);
SchedulerUtils.repeat(20, MakeInvulnerable::updateInvul); //SchedulerUtils.repeat(20, MakeInvulnerable::updateInvul);
SchedulerUtils.repeat(100, GenFunctions::tickGens); SchedulerUtils.repeat(200, GenFunctions::tickGens);
log.info(""" log.info("""
\n,ggggggggggg, \s \n,ggggggggggg, \s
@@ -106,9 +109,13 @@ public final class SSSBliss extends JavaPlugin {
// Init // Init
mainConfig = JsonSerializable.load(cfgfile, MainConfig.class, new MainConfig()); mainConfig = JsonSerializable.load(cfgfile, MainConfig.class, new MainConfig());
//storage = Serializable.load(storageFile, GenStorage.class, new GenStorage());
loadStorage();
// Save // Save
mainConfig.save(); mainConfig.save();
//storage.save(storageFile);
saveStorage();
log.info("Loading Dictionary (" + SSSBliss.mainConfig.plugin.lang + ")..."); log.info("Loading Dictionary (" + SSSBliss.mainConfig.plugin.lang + ")...");
@@ -123,6 +130,7 @@ public final class SSSBliss extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
// Plugin shutdown logic // Plugin shutdown logic
saveStorage();
log.info("SSSBliss has disabled! (" + getDescription().getVersion() + ") Your server is now no longer protected!"); 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; 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();
}
}
} }

View File

@@ -191,7 +191,7 @@ public class SSSBlissCommand implements CustomCommand {
@Override @Override
public void dispatchCompletions(CompletionBuilder b) { 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("reload"));
b.then(b.arg("gems").then( b.then(b.arg("gems").then(
b.arg("give") b.arg("give")
@@ -201,9 +201,9 @@ public class SSSBlissCommand implements CustomCommand {
b.arg("encrypt","lang","toggle"))); b.arg("encrypt","lang","toggle")));
} }
private final GenInfo ironGen = new GenInfo(Material.IRON_BLOCK.name(),Material.IRON_ORE.name()); private final GenInfo ironGen = new GenInfo(Material.IRON_ORE.name(),Material.IRON_INGOT.name());
private final GenInfo diamondGen = new GenInfo(Material.DIAMOND_BLOCK.name(),Material.DIAMOND_ORE.name()); private final GenInfo diamondGen = new GenInfo(Material.DIAMOND_ORE.name(),Material.DIAMOND.name());
private final GenInfo goldGen = new GenInfo(Material.GOLD_BLOCK.name(),Material.GOLD_ORE.name()); private final GenInfo goldGen = new GenInfo(Material.GOLD_ORE.name(),Material.GOLD_INGOT.name());
private final GenInfo coalGen = new GenInfo(Material.COAL_BLOCK.name(),Material.COAL_ORE.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.DRIED_KELP_BLOCK.name()); private final GenInfo kelpGen = new GenInfo(Material.DRIED_KELP_BLOCK.name(),Material.KELP.name());
} }

View File

@@ -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);
}
}

View File

@@ -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();
}
}
}

View File

@@ -11,7 +11,9 @@ import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType; 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"); 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() { public ItemStack getItem() {
ItemStack genItem = new ItemBuilder() ItemStack genItem = new ItemBuilder()
.material(getBlock(block)) .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())))) .lore(Text.color("&7Drops 1x %s".formatted(Text.cleanName(getBlock(drop).name()))))
.build(); .build();
updateItem(genItem); updateItem(genItem);

View File

@@ -1,19 +1,9 @@
package io.github.thetrouper.sssbliss.data; package io.github.thetrouper.sssbliss.data;
import io.github.itzispyder.pdk.utils.misc.JsonSerializable; import java.io.Serializable;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class GenStorage implements JsonSerializable<GenStorage> { public class GenStorage implements Serializable {
@Override
public File getFile() {
File file = new File("plugins/SSSBliss/storage.json");
file.getParentFile().mkdirs();
return file;
}
public List<PlacedGen> gens = new ArrayList<>(); public List<PlacedGen> gens = new ArrayList<>();
} }

View File

@@ -5,7 +5,9 @@ import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.inventory.ItemStack; 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() { public Location getLocation() {
return new Location(getWorld(), x, y, z); 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() { public void drop() {
var loc = getLocation(); var loc = getLocation();
var item = new ItemStack(GenInfo.getBlock(info.drop())); 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);
} }
} }

View File

@@ -12,7 +12,7 @@ public class GenBreakEvent implements CustomListener {
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
public void onBreak(BlockBreakEvent e) { public void onBreak(BlockBreakEvent e) {
e.getPlayer().sendMessage("broke block"); //e.getPlayer().sendMessage("broke block");
if (e.isCancelled()) if (e.isCancelled())
return; return;
@@ -24,7 +24,7 @@ public class GenBreakEvent implements CustomListener {
e.setCancelled(true); e.setCancelled(true);
b.setType(Material.AIR); b.setType(Material.AIR);
SSSBliss.storage.gens.remove(gen); SSSBliss.storage.gens.remove(gen);
SSSBliss.storage.save(); SSSBliss.saveStorage();
ItemStack drop = gen.info().getItem(); ItemStack drop = gen.info().getItem();
loc.getWorld().dropItem(loc,drop); loc.getWorld().dropItem(loc,drop);

View File

@@ -17,7 +17,7 @@ public class GenPlaceEvent implements CustomListener {
@EventHandler @EventHandler
public void placeGen(BlockPlaceEvent e) { public void placeGen(BlockPlaceEvent e) {
e.getPlayer().sendMessage("placed block"); //e.getPlayer().sendMessage("placed block");
Player p = e.getPlayer(); Player p = e.getPlayer();
Block b = e.getBlock(); Block b = e.getBlock();
ItemStack i = e.getItemInHand(); ItemStack i = e.getItemInHand();
@@ -28,7 +28,7 @@ public class GenPlaceEvent implements CustomListener {
b.setType(GenInfo.getBlock(gen.block())); b.setType(GenInfo.getBlock(gen.block()));
PlacedGen newGen = new PlacedGen(b.getWorld().getName(),b.getX(),b.getY(),b.getZ(),gen); PlacedGen newGen = new PlacedGen(b.getWorld().getName(),b.getX(),b.getY(),b.getZ(),gen);
SSSBliss.storage.gens.add(newGen); 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())))); 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); DisplayUtils.vortex(b.getLocation(),0,DisplayUtils.DUST_PARTICLE_FACTORY.apply(Color.AQUA,0.5F),0.1,0.1,2);
} }

View File

@@ -8,6 +8,7 @@ import java.util.function.Consumer;
public class GenFunctions { public class GenFunctions {
public static void tickGens() { public static void tickGens() {
if (SSSBliss.storage.gens == null || SSSBliss.storage.gens.isEmpty()) return;
accept(GenFunctions::genAction); accept(GenFunctions::genAction);
} }
@@ -16,6 +17,7 @@ public class GenFunctions {
} }
public static void genAction(PlacedGen gen) { public static void genAction(PlacedGen gen) {
if (!gen.getLocation().isChunkLoaded()) return;
gen.drop(); gen.drop();
} }
} }