save info to data file

This commit is contained in:
MidnightTale 2023-04-25 23:16:55 +07:00
commit 9484c2d667

View file

@ -5,6 +5,7 @@ import io.papermc.paper.threadedregions.scheduler.RegionScheduler;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.data.type.Slab; import org.bukkit.block.data.type.Slab;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.BlockDisplay; import org.bukkit.entity.BlockDisplay;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
@ -15,6 +16,8 @@ import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import java.io.File;
import java.io.IOException;
import java.util.*; import java.util.*;
public class FoliaFlow extends JavaPlugin implements Listener { public class FoliaFlow extends JavaPlugin implements Listener {
private final double vh = 0.2; private final double vh = 0.2;
@ -69,30 +72,46 @@ public class FoliaFlow extends JavaPlugin implements Listener {
}, 20L, 1L); }, 20L, 1L);
RegionScheduler schedulerdisplay = getServer().getRegionScheduler(); File dataFile = new File(getDataFolder(), "data.yml");
String tag = "FoliaFlow_Display"; YamlConfiguration config = YamlConfiguration.loadConfiguration(dataFile);
schedulerdisplay.run(this, Objects.requireNonNull(Bukkit.getWorld("world_the_end")), 1, 1, (schedulerTask) -> { // Get the existing flag value, or default to false if it doesn't exist
World endWorld = getServer().getWorld("world_the_end"); boolean entitySpawned = config.getBoolean("entity_spawned", false);
assert endWorld != null;
Chunk chunk = endWorld.getChunkAt(0, 0);
if (!chunk.isLoaded()) {
chunk.load();
getLogger().info("Loaded Chunk");
}
// Check if a block display entity with the specified tag already exists // If the entity has not already been spawned, spawn it and set the flag to true
boolean displayExists = endWorld.getEntitiesByClass(BlockDisplay.class).stream() if (!entitySpawned) {
.anyMatch(entity -> entity.getScoreboardTags().contains(tag)); RegionScheduler schedulerdisplay = getServer().getRegionScheduler();
String tag = "FoliaFlow_Display";
// Spawn a new block display entity only if it doesn't already exist schedulerdisplay.run(this, Objects.requireNonNull(Bukkit.getWorld("world_the_end")), 1, 1, (schedulerTask) -> {
if (!displayExists) { World endWorld = getServer().getWorld("world_the_end");
BlockDisplay display = (BlockDisplay) endWorld.spawnEntity(new Location(endWorld, 100.0005, 48, -0.0005), EntityType.BLOCK_DISPLAY); assert endWorld != null;
display.setBlock(Bukkit.createBlockData(Material.OBSIDIAN)); Chunk chunk = endWorld.getChunkAt(0, 0);
display.addScoreboardTag(tag); if (!chunk.isLoaded()) {
getLogger().info("Setup BlockDisplay" + "(" + tag + ") " + display); chunk.load();
} getLogger().info("Loaded Chunk");
}); }
boolean displayExists = endWorld.getEntitiesByClass(BlockDisplay.class).stream()
.anyMatch(entity -> entity.getScoreboardTags().contains(tag));
getLogger().info("Exist BlockDisplay" + "(" + tag + ")");
if (!displayExists) {
BlockDisplay display = (BlockDisplay) endWorld.spawnEntity(new Location(endWorld, 100.0005, 48, -0.0005), EntityType.BLOCK_DISPLAY);
display.setBlock(Bukkit.createBlockData(Material.OBSIDIAN));
display.addScoreboardTag(tag);
getLogger().info("Setup BlockDisplay" + "(" + tag + ") " + display);
// Set the flag to indicate that the entity has been spawned
config.set("entity_spawned", true);
try {
config.save(dataFile);
} catch (IOException e) {
getLogger().warning("Failed to save plugin data file");
}
}
});
}
getServer().getPluginManager().registerEvents(this, this); getServer().getPluginManager().registerEvents(this, this);
@ -133,7 +152,7 @@ public class FoliaFlow extends JavaPlugin implements Listener {
dummy.setVelocity(dummyVel); dummy.setVelocity(dummyVel);
} }
} catch (NullPointerException dummy) { } catch (NullPointerException dummy) {
getServer().getLogger().info("onFallingBlockToBlock error (likly chunky it not load)"); getServer().getLogger().info("onFallingBlockToBlock error (likely chunky it not load)");
} }
} }
} }