diff --git a/.idea/misc.xml b/.idea/misc.xml index 5cd9a10..f16dea7 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,9 @@ + + + diff --git a/src/main/java/eu/mhsl/minecraft/WorldMuseum/Main.java b/src/main/java/eu/mhsl/minecraft/WorldMuseum/Main.java index a5b3374..585d60d 100644 --- a/src/main/java/eu/mhsl/minecraft/WorldMuseum/Main.java +++ b/src/main/java/eu/mhsl/minecraft/WorldMuseum/Main.java @@ -14,9 +14,11 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; +import java.util.logging.Logger; public class Main extends JavaPlugin { private static Main instance; + private static final Logger LOGGER = Bukkit.getLogger(); private final List worlds = new ArrayList<>(); @@ -50,13 +52,11 @@ public class Main extends JavaPlugin { //noinspection ResultOfMethodCallIgnored worldFolder.mkdirs(); Arrays.stream(Objects.requireNonNull(worldFolder.listFiles())).forEach(file -> { - Bukkit.getLogger().info("[Worldmuseum] Loading world " + file.getName()); + LOGGER.info("[Worldmuseum] Loading world " + file.getName()); if (file.isDirectory()) this.worlds.add(new ViewableWorld(file)); - else Bukkit.getLogger().info( "[Worldmuseum] " + file.getName() + " is not a valid world!"); + else LOGGER.info( "[Worldmuseum] " + file.getName() + " is not a valid world!"); }); this.worlds.forEach(ViewableWorld::loadWorld); - for(ViewableWorld viewableWorld : this.worlds) viewableWorld.getWorld().setAutoSave(false); - } public static Main instance() { diff --git a/src/main/java/eu/mhsl/minecraft/WorldMuseum/listener/PlayerListener.java b/src/main/java/eu/mhsl/minecraft/WorldMuseum/listener/PlayerListener.java index a1d04ac..17af1e5 100644 --- a/src/main/java/eu/mhsl/minecraft/WorldMuseum/listener/PlayerListener.java +++ b/src/main/java/eu/mhsl/minecraft/WorldMuseum/listener/PlayerListener.java @@ -4,17 +4,24 @@ import eu.mhsl.minecraft.WorldMuseum.Main; import net.kyori.adventure.util.Ticks; import org.bukkit.Bukkit; import org.bukkit.GameMode; +import org.bukkit.Material; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; +import java.util.Objects; + public class PlayerListener implements Listener { @EventHandler public void onJoin(PlayerJoinEvent event) { event.getPlayer().setGameMode(GameMode.SURVIVAL); + Main.instance().getWorlds().forEach(world -> { + if (world.isDefaultWorld()) world.addViewer(event.getPlayer()); + }); event.getPlayer().addPotionEffect( new PotionEffect(PotionEffectType.HASTE, PotionEffect.INFINITE_DURATION, 15, false, false, false) ); @@ -22,10 +29,15 @@ public class PlayerListener implements Listener { @EventHandler public void onBreak(BlockBreakEvent event) { + Material material = event.getBlock().getType(); Bukkit.getScheduler().scheduleSyncDelayedTask( Main.instance(), - () -> event.getBlock().getWorld().getBlockAt(event.getBlock().getLocation()).setType(event.getBlock().getType()), - Ticks.TICKS_PER_SECOND * 5 + () -> Objects.requireNonNull(Bukkit.getWorld(event.getBlock().getWorld().getName())).setType(event.getBlock().getLocation(), material), Ticks.TICKS_PER_SECOND * 5 ); } + + @EventHandler + public void onPlace(BlockPlaceEvent event) { + event.setCancelled(true); + } } diff --git a/src/main/java/eu/mhsl/minecraft/WorldMuseum/viewableWorld/ViewableWorld.java b/src/main/java/eu/mhsl/minecraft/WorldMuseum/viewableWorld/ViewableWorld.java index 445e48c..eb4964f 100644 --- a/src/main/java/eu/mhsl/minecraft/WorldMuseum/viewableWorld/ViewableWorld.java +++ b/src/main/java/eu/mhsl/minecraft/WorldMuseum/viewableWorld/ViewableWorld.java @@ -21,6 +21,7 @@ public class ViewableWorld { private boolean enabled = true; private String name; private Material icon = Material.GRASS_BLOCK; + private boolean isDefaultWorld = false; public ViewableWorld(File worldLocation) { this.worldLocation = worldLocation; @@ -31,14 +32,17 @@ public class ViewableWorld { this.enabled = config.getBoolean("enabled"); this.name = config.getString("name"); this.icon = Material.valueOf(config.getString("icon")); + this.isDefaultWorld = config.getBoolean("isDefaultWorld"); } else { System.out.println("[WorldMuseum] Could not find config.yml"); try { + //noinspection ResultOfMethodCallIgnored configFile.createNewFile(); FileConfiguration data = YamlConfiguration.loadConfiguration(configFile); data.set("enabled", false); data.set("name", this.name); data.set("icon", this.icon.name()); + data.set("isDefaultWorld", false); data.save(configFile); } catch (IOException e) {e.printStackTrace();} @@ -53,6 +57,7 @@ public class ViewableWorld { this.world = worldCreator.createWorld(); Objects.requireNonNull(this.world); this.world.setAutoSave(false); +// Bukkit.unloadWorld(this.world, false); } public void unloadWorld() { @@ -76,7 +81,7 @@ public class ViewableWorld { return enabled; } - public World getWorld() { - return world; + public boolean isDefaultWorld() { + return isDefaultWorld; } }