This commit is contained in:
天クマ 2026-05-30 20:35:24 -03:00
commit dcff9c656d
19 changed files with 1262 additions and 0 deletions

View file

@ -0,0 +1,46 @@
package org.adrianvictor.sweetdreams;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
public class Configuration {
private final YamlConfiguration playerStorage = new YamlConfiguration();
private final File playerStorageFile;
private final YamlConfiguration pluginConfig = new YamlConfiguration();
private final File pluginConfigFile = new File("");
private final SweetDreams plugin;
public Configuration() {
plugin = SweetDreams.getPlugin();
playerStorageFile = new File(plugin.getDataFolder(), "playerstorage.yml");
if (playerStorageFile.exists()) {
try {
playerStorage.load(playerStorageFile);
} catch (IOException | InvalidConfigurationException e) {
plugin.getLogger().severe("Error loading player storage: " + e.getMessage());
throw new RuntimeException(e);
}
}
}
public void savePlayerStorage() {
try {
playerStorage.save(playerStorageFile);
} catch (IOException e) {
plugin.getLogger().severe("Could not save to player storage: " + e.getMessage());
throw new RuntimeException(e);
}
}
public YamlConfiguration getPlayerStorage() {
return playerStorage;
}
public YamlConfiguration getPluginConfig() {
return pluginConfig;
}
}

View file

@ -0,0 +1,122 @@
package org.adrianvictor.sweetdreams;
import com.destroystokyo.paper.event.player.PlayerSetSpawnEvent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityPortalEnterEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.util.Vector;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
public class EventListener implements Listener {
private final PlayerStorage storage;
private final Set<UUID> teleporting = new HashSet<>();
public EventListener() {
this.storage = SweetDreams.getPlugin().getStorage();
}
@EventHandler
public void onPlayerBedEnterEvent(PlayerBedEnterEvent event) {
if (event.getPlayer().getWorld() == SweetDreams.getPlugin().getWorld()) {
storage.savePlayerSpawnPoint(event.getPlayer());
event.getPlayer().sendMessage("Respawn point set.");
event.setCancelled(true);
return;
}
Long time = event.getPlayer().getWorld().getTime();
if (!(time >= 13000 && time <= 23000)) {
event.setCancelled(true);
return;
}
teleport(event.getPlayer());
event.setCancelled(true);
}
@EventHandler
public void onPlayerSetSpawn(PlayerSetSpawnEvent event) {
if (event.getPlayer().getWorld() != SweetDreams.getPlugin().getWorld())
return;
// TODO save player spawn point on dreamlands
event.setCancelled(true);
}
@EventHandler
public void onPortalEnter(EntityPortalEnterEvent event) {
if (event.getEntity().getWorld() == SweetDreams.getPlugin().getWorld())
event.setCancelled(true);
}
@EventHandler
public void onEntityDamage(EntityDamageEvent event) {
if (event.getEntity().getWorld() != SweetDreams.getPlugin().getWorld()) return;
if (event.getEntity() instanceof Player player) {
if (event.getCause() == EntityDamageEvent.DamageCause.VOID) {
teleport(player);
player.setVelocity(new Vector(0, 0, 0));
player.setFallDistance(0);
}
}
}
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event) {
if (event.getEntity().getWorld() != SweetDreams.getPlugin().getWorld()) return;
Player player = event.getPlayer();
teleport(player);
event.setCancelled(true);
}
private void teleport(Player player) {
UUID uuid = player.getUniqueId();
if (!teleporting.add(uuid)) return;
Bukkit.getScheduler().runTask(SweetDreams.getPlugin(), () -> {
try {
World playerWorld = player.getWorld();
if (playerWorld == SweetDreams.getPlugin().getWorld()) {
storage.savePlayer(player, PlayerStorage.StorageWorldType.SKYLANDS);
player.teleport(Objects.requireNonNullElse(
player.getRespawnLocation(),
Bukkit.getWorld("world").getSpawnLocation()
));
player.getInventory().clear();
storage.loadPlayer(player, PlayerStorage.StorageWorldType.OVERWORLD);
player.sendMessage("You woke up from a bad dream...");
player.playSound(player.getLocation(), Sound.ENTITY_ENDER_DRAGON_DEATH, 1.0f, 1.0f);
} else {
World world = SweetDreams.getPlugin().getWorld();
storage.savePlayer(player, PlayerStorage.StorageWorldType.OVERWORLD);
player.getInventory().clear();
player.teleport(storage.loadPlayer(player, PlayerStorage.StorageWorldType.SKYLANDS));
}
} finally {
teleporting.remove(uuid);
}
});
}
}

View file

@ -0,0 +1,140 @@
package org.adrianvictor.sweetdreams;
import org.bukkit.Location;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.UUID;
public class PlayerStorage {
private final Configuration config;
private final YamlConfiguration storage;
// Map<UUID, ItemStack[]> inventories = new HashMap<>();
public enum StorageWorldType {
OVERWORLD("overworld"),
SKYLANDS("skylands");
private final String key;
StorageWorldType(String key) {
this.key = key;
}
public String getKey() {
return key;
}
}
public PlayerStorage(Configuration config) {
this.config = config;
this.storage = config.getPlayerStorage();
}
public void savePlayer(Player player, StorageWorldType type) {
UUID uuid = player.getUniqueId();
String base = "players." + uuid + "." + type.getKey();
ItemStack[] inv = player.getInventory().getContents();
storage.set(base + ".inventory", null);
for (int i = 0; i < inv.length; i++) {
storage.set(base + ".inventory." + i, inv[i]);
}
ItemStack[] armor = player.getInventory().getArmorContents();
storage.set(base + ".armor", null);
for (int i = 0; i < armor.length; i++) {
storage.set(base + ".armor." + i, armor[i]);
}
storage.set(base + ".offhand", player.getInventory().getItemInOffHand());
ItemStack[] ender = player.getEnderChest().getContents();
storage.set(base + ".enderchest", null);
for (int i = 0; i < ender.length; i++) {
storage.set(base + ".enderchest." + i, ender[i]);
}
storage.set(base + ".xp.level", player.getLevel());
storage.set(base + ".xp.exp", player.getExp());
storage.set(base + ".xp.total", player.getTotalExperience());
storage.set(base + ".stats.health", player.getHealth());
storage.set(base + ".stats.food", player.getFoodLevel());
storage.set(base + ".stats.saturation", player.getSaturation());
config.savePlayerStorage();
}
public Location loadPlayer(Player player, StorageWorldType type) {
UUID uuid = player.getUniqueId();
String base = "players." + uuid + "." + type.getKey();
ItemStack[] inv = new ItemStack[36];
var invSec = storage.getConfigurationSection(base + ".inventory");
if (invSec != null) {
for (String key : invSec.getKeys(false)) {
try {
int slot = Integer.parseInt(key);
if (slot >= 0 && slot < inv.length) {
inv[slot] = storage.getItemStack(base + ".inventory." + key);
}
} catch (Exception ignored) {}
}
}
player.getInventory().setContents(inv);
ItemStack[] armor = new ItemStack[4];
var armorSec = storage.getConfigurationSection(base + ".armor");
if (armorSec != null) {
for (String key : armorSec.getKeys(false)) {
try {
int slot = Integer.parseInt(key);
if (slot >= 0 && slot < armor.length) {
armor[slot] = storage.getItemStack(base + ".armor." + key);
}
} catch (Exception ignored) {}
}
}
player.getInventory().setArmorContents(armor);
ItemStack offhand = storage.getItemStack(base + ".offhand");
player.getInventory().setItemInOffHand(offhand);
ItemStack[] ender = new ItemStack[27];
var enderSec = storage.getConfigurationSection(base + ".enderchest");
if (enderSec != null) {
for (String key : enderSec.getKeys(false)) {
try {
int slot = Integer.parseInt(key);
if (slot >= 0 && slot < ender.length) {
ender[slot] = storage.getItemStack(base + ".enderchest." + key);
}
} catch (Exception ignored) {}
}
}
player.getEnderChest().setContents(ender);
player.setTotalExperience(storage.getInt(base + ".xp.total", 0));
player.setLevel(storage.getInt(base + ".xp.level", 0));
player.setExp((float) storage.getDouble(base + ".xp.exp", 0.0));
player.setHealth(Math.min(storage.getDouble(base + ".stats.health", 20.0), player.getMaxHealth()));
player.setFoodLevel(storage.getInt(base + ".stats.food", 20));
player.setSaturation((float) storage.getDouble(base + ".stats.saturation", 5.0));
Object rawSpawn = storage.get(base + ".spawn");
if (rawSpawn instanceof Location spawn) {
return spawn;
} else {
return SweetDreams.getPlugin().getWorld().getSpawnLocation();
}
}
public void savePlayerSpawnPoint(Player player) {
UUID uuid = player.getUniqueId();
String base = "players." + uuid + ".skylands";
storage.set(base + ".spawn", player.getLocation());
}
}

View file

@ -0,0 +1,398 @@
package org.adrianvictor.sweetdreams;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.generator.BlockPopulator;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.generator.WorldInfo;
import org.jspecify.annotations.NonNull;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
public class SkyIslandsGenerator extends ChunkGenerator {
private enum IslandType {
ROUND,
LONG,
ARCHIPELAGO,
SPIKY
}
private static final IslandType[] TYPES = IslandType.values();
private record Blob(double x, double y, double z, double rx, double ry, double rz) {}
private static final int CELL_SIZE = 30;
private static final int MIN_RADIUS = 6;
private static final int MAX_RADIUS = 16;
private static final int MIN_Y = 80;
private static final int MAX_Y = 180;
private record LeafOffset(int dx, int dy, int dz) {}
private static final LeafOffset[] LEAF_OFFSETS;
static {
java.util.List<LeafOffset> offsets = new java.util.ArrayList<>();
for (int dx = -2; dx <= 2; dx++) {
for (int dz = -2; dz <= 2; dz++) {
for (int dy = -2; dy <= 0; dy++) {
if (Math.abs(dx) + Math.abs(dz) <= 3) {
offsets.add(new LeafOffset(dx, dy, dz));
}
}
}
}
LEAF_OFFSETS = offsets.toArray(LeafOffset[]::new);
}
@Override
public @NonNull List<BlockPopulator> getDefaultPopulators(@NonNull World world) {
return Collections.singletonList(new SkylandsBlockPopulator());
}
@Override
public void generateSurface(WorldInfo worldInfo, @NonNull Random random, int chunkX, int chunkZ, @NonNull ChunkData chunkData) {
long seed = worldInfo.getSeed();
int chunkWorldX = chunkX * 16;
int chunkWorldZ = chunkZ * 16;
int cellX = Math.floorDiv(chunkWorldX, CELL_SIZE);
int cellZ = Math.floorDiv(chunkWorldZ, CELL_SIZE);
short[][] highestY = new short[16][16];
AtomicBoolean hasTerrain = new AtomicBoolean(false);
for (int x = 0; x < 16; x++) {
Arrays.fill(highestY[x], Short.MIN_VALUE);
}
for (int gx = cellX - 2; gx <= cellX + 2; gx++) {
for (int gz = cellZ - 2; gz <= cellZ + 2; gz++) {
SplittableRandom r = new SplittableRandom(hash(seed, gx, gz));
if (r.nextDouble() > 0.8) {
continue;
}
generateIslandGeometry(
gx,
gz,
r,
blob -> hasTerrain.set(hasTerrain.get() | carveBlob(
chunkData,
chunkX,
chunkZ,
blob,
highestY
))
);
}
}
if (!hasTerrain.get()) {
return;
}
decorateTerrain(chunkData, random, highestY);
}
private boolean carveBlob(ChunkData chunkData, int chunkX, int chunkZ, Blob blob, short[][] highestY) {
boolean touched = false;
int minHeight = chunkData.getMinHeight();
int maxHeight = chunkData.getMaxHeight() - 1;
double invRx = 1.0 / blob.rx();
double invRy = 1.0 / blob.ry();
double invRz = 1.0 / blob.rz();
int baseX = chunkX * 16;
int baseZ = chunkZ * 16;
int blobMinX = (int)Math.floor(blob.x() - blob.rx());
int blobMaxX = (int)Math.ceil(blob.x() + blob.rx());
int blobMinZ = (int)Math.floor(blob.z() - blob.rz());
int blobMaxZ = (int)Math.ceil(blob.z() + blob.rz());
int minLocalX = Math.max(0, blobMinX - baseX);
int maxLocalX = Math.min(15, blobMaxX - baseX);
int minLocalZ = Math.max(0, blobMinZ - baseZ);
int maxLocalZ = Math.min(15, blobMaxZ - baseZ);
for (int localX = minLocalX; localX <= maxLocalX; localX++) {
for (int localZ = minLocalZ; localZ <= maxLocalZ; localZ++) {
int worldX = baseX + localX;
int worldZ = baseZ + localZ;
// int minY = Math.max(chunkData.getMinHeight(), (int)Math.floor(blob.y() - blob.ry()));
// int maxY = Math.min(chunkData.getMaxHeight() -1, (int)Math.ceil(blob.y() + blob.ry()));
double nx = (worldX - blob.x()) * invRx;
double nz = (worldZ - blob.z()) * invRz;
double horizontal = nx * nx + nz * nz;
if (horizontal > 1.0)
continue;
double remaining = 1.0 - horizontal;
if (remaining <= 0)
continue;
double yRadius = Math.sqrt(remaining) * blob.ry();
int startY = Math.max(
minHeight,
(int)Math.ceil(blob.y() - yRadius)
);
int endY = Math.min(
maxHeight,
(int)Math.floor(blob.y() + yRadius)
);
for (int y = startY; y <= endY; y++) {
chunkData.setBlock(localX, y, localZ, Material.STONE);
touched = true;
}
highestY[localX][localZ] = (short) Math.max(highestY[localX][localZ], endY);
}
}
return touched;
}
private void decorateTerrain(ChunkData chunkData, Random random, short[][] highestY) {
generateSurfaceLayers(chunkData, highestY);
generateOres(chunkData, random);
generateTrees(chunkData, random, highestY);
}
private void generateSurfaceLayers(ChunkData chunkData, short[][] highestY) {
int minHeight = chunkData.getMinHeight();
int maxHeight = chunkData.getMaxHeight() - 1;
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
int highest = highestY[x][z];
if (highest == Short.MIN_VALUE)
continue;
chunkData.setBlock(x, highest, z, Material.GRASS_BLOCK);
for (int y = highest - 1; y >= highest - 3; y--) {
if (y >= minHeight) {
chunkData.setBlock(x, y, z, Material.DIRT);
}
}
}
}
}
private void generateOres(ChunkData chunkData, Random random) {
generateOreVeins(chunkData, random, Material.COAL_ORE, 25, 10);
generateOreVeins(chunkData, random, Material.IRON_ORE, 15, 8);
generateOreVeins(chunkData, random, Material.GOLD_ORE, 6, 6);
generateOreVeins(chunkData, random, Material.DIAMOND_ORE, 2, 4);
}
private void generateOreVeins(ChunkData chunkData, Random random, Material ore, int veins, int size) {
for (int i = 0; i < veins; i++) {
int startX = random.nextInt(16);
int startY = chunkData.getMinHeight() + random.nextInt(chunkData.getMaxHeight() - chunkData.getMinHeight());
int startZ = random.nextInt(16);
for (int j = 0; j < size; j++) {
int x = startX + random.nextInt(5) - 2;
int y = startY + random.nextInt(5) - 2;
int z = startZ + random.nextInt(5) - 2;
if (x < 0 || x >= 16) continue;
if (z < 0 || z >= 16) continue;
if (y < chunkData.getMinHeight()) continue;
if (y >= chunkData.getMaxHeight()) continue;
if (chunkData.getType(x, y, z) == Material.STONE) {
chunkData.setBlock(x, y, z, ore);
}
}
}
}
private void generateTrees(ChunkData chunkData, Random random, short[][] highestY) {
for (int x = 2; x < 14; x++) {
for (int z = 2; z < 14; z++) {
if (random.nextInt(500) != 0) {
continue;
}
int y = highestY[x][z];
if (y == Short.MIN_VALUE) {
continue;
}
if (chunkData.getType(x, y, z) != Material.GRASS_BLOCK) {
continue;
}
placeTree(chunkData, x, y + 1, z, random);
}
}
}
private void placeTree(ChunkData chunkData, int x, int y, int z, Random random) {
int height = 4 + random.nextInt(3);
for (int dy = 0; dy < height; dy++) {
chunkData.setBlock(x, y + dy, z, Material.OAK_LOG);
}
int top = y + height;
for (LeafOffset o : LEAF_OFFSETS) {
chunkData.setBlock(
x + o.dx(),
top + o.dy(),
z + o.dz(),
Material.OAK_LEAVES
);
}
chunkData.setBlock(x, top + 1, z, Material.OAK_LEAVES);
}
private void generateIslandGeometry(
int cellX,
int cellZ,
SplittableRandom random,
java.util.function.Consumer<Blob> consumer
) {
int centerX = cellX * CELL_SIZE + random.nextInt(CELL_SIZE);
int centerZ = cellZ * CELL_SIZE + random.nextInt(CELL_SIZE);
int centerY = MIN_Y + random.nextInt(MAX_Y - MIN_Y);
IslandType type =
TYPES[random.nextInt(TYPES.length)];
switch (type) {
case ROUND -> {
consumer.accept(
new Blob(
centerX,
centerY,
centerZ,
12 + random.nextInt(10),
6 + random.nextInt(5),
12 + random.nextInt(10)
)
);
}
case LONG -> {
double angle = random.nextDouble() * Math.PI * 2;
int segments = 3 + random.nextInt(5);
for (int i = 0; i < segments; i++) {
consumer.accept(
new Blob(
centerX + Math.cos(angle) * i * 10,
centerY + random.nextInt(5) - 2,
centerZ + Math.sin(angle) * i * 10,
8 + random.nextInt(4),
5 + random.nextInt(2),
8 + random.nextInt(4)
)
);
}
}
case ARCHIPELAGO -> {
int count = 3 + random.nextInt(5);
for (int i = 0; i < count; i++) {
consumer.accept(
new Blob(
centerX + random.nextInt(60) - 30,
centerY,
centerZ + random.nextInt(60) - 30,
7 + random.nextInt(5),
4 + random.nextInt(3),
7 + random.nextInt(5)
)
);
}
}
case SPIKY -> {
consumer.accept(
new Blob(centerX, centerY, centerZ, 16, 8, 16)
);
for (int i = 0; i < 6; i++) {
consumer.accept(
new Blob(
centerX + random.nextInt(20) - 10,
centerY - random.nextInt(12),
centerZ + random.nextInt(20) - 10,
2,
10 + random.nextInt(8),
2
)
);
}
}
}
}
private static long hash(long seed, int x, int z) {
long h = seed;
h ^= (long) x * 0x9E3779B97F4A7C15L;
h ^= (long) z * 0xC2B2AE3D27D4EB4FL;
h ^= h >>> 30;
h *= 0xBF58476D1CE4E5B9L;
h ^= h >>> 27;
h *= 0x94D049BB133111EBL;
h ^= h >>> 31;
return h;
}
}

View file

@ -0,0 +1,22 @@
package org.adrianvictor.sweetdreams;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.generator.BlockPopulator;
import org.bukkit.generator.LimitedRegion;
import org.bukkit.generator.WorldInfo;
import org.jspecify.annotations.NonNull;
import java.util.Random;
public class SkylandsBlockPopulator extends BlockPopulator {
@Override
public void populate(
@NonNull WorldInfo worldInfo,
@NonNull Random random,
int chunkX,
int chunkZ,
@NonNull LimitedRegion region
) {
}
}

View file

@ -0,0 +1,86 @@
package org.adrianvictor.sweetdreams;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
public final class SweetDreams extends JavaPlugin {
private World world;
private static SweetDreams plugin;
private Configuration configuration;
private PlayerStorage storage;
@Override
public void onEnable() {
plugin = this;
configuration = new Configuration();
storage = new PlayerStorage(configuration);
boolean generated = false;
for (World world: getServer().getWorlds()) {
if (world.getName().equals("world_sweetdreams")) {
Bukkit.unloadWorld(world, true);
File worldFolder = new File(Bukkit.getWorldContainer(), "world_sweetdreams");
if (worldFolder.exists()) {
deleteFolder(worldFolder);
}
// this.world = world;
// generated = true;
}
}
if (!generated) {
world = createSkylandsWorld("world_sweetdreams");
}
getServer().getPluginManager().registerEvents(new EventListener(), this);
}
private void deleteFolder(File folder) {
if (folder.isDirectory()) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
deleteFolder(file);
}
}
}
folder.delete();
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
public World getWorld() {
return world;
}
public static SweetDreams getPlugin() {
return plugin;
}
public World createSkylandsWorld(String worldName) {
WorldCreator creator = new WorldCreator(worldName);
creator.generator(new SkyIslandsGenerator());
creator.environment(World.Environment.NORMAL);
World world = creator.createWorld();
world.setSpawnFlags(true, true);
return world;
}
public Configuration getConfiguration() {
return configuration;
}
public PlayerStorage getStorage() {
return storage;
}
}

View file

View file

View file

@ -0,0 +1,11 @@
name: SweetDreams
description: $description
prefix: SweetDreams
version: '${version}'
main: org.adrianvictor.sweetdreams.SweetDreams
api-version: '1.21.11'
load: POSTWORLD
authors: [ tenkuma ]
website: https://adrianvic.github.io