basic world loading implemented
This commit is contained in:
parent
bf1fe7cd3a
commit
45a4349c2d
5
.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
5
.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<component name="ProjectCodeStyleConfiguration">
|
||||||
|
<state>
|
||||||
|
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
|
||||||
|
</state>
|
||||||
|
</component>
|
@ -23,4 +23,11 @@ java {
|
|||||||
|
|
||||||
tasks.test {
|
tasks.test {
|
||||||
useJUnitPlatform()
|
useJUnitPlatform()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tasks.register<Exec>("copyJarToTestServer") {
|
||||||
|
dependsOn(tasks.jar)
|
||||||
|
mustRunAfter(tasks.jar)
|
||||||
|
|
||||||
|
commandLine("cp", "build/libs/WorldMuseum-1.0-SNAPSHOT.jar", "/home/elias/Dokumente/mcTestServer/plugins/worldmuseum.jar")
|
||||||
|
}
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
package eu.mhsl.minecraft.WorldMuseum;
|
||||||
|
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.block.BlockRedstoneEvent;
|
||||||
|
|
||||||
|
public class CancelListener implements Listener {
|
||||||
|
@EventHandler
|
||||||
|
public void disableRedstone(BlockRedstoneEvent event) {
|
||||||
|
event.setNewCurrent(0);
|
||||||
|
}
|
||||||
|
}
|
52
src/main/java/eu/mhsl/minecraft/WorldMuseum/Main.java
Normal file
52
src/main/java/eu/mhsl/minecraft/WorldMuseum/Main.java
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package eu.mhsl.minecraft.WorldMuseum;
|
||||||
|
|
||||||
|
import eu.mhsl.minecraft.WorldMuseum.worldSelector.WorldSelectListener;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class Main extends JavaPlugin {
|
||||||
|
private static Main instance;
|
||||||
|
|
||||||
|
private final List<ViewableWorld> worlds = new ArrayList<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onEnable() {
|
||||||
|
Main.instance = this;
|
||||||
|
this.saveDefaultConfig();
|
||||||
|
|
||||||
|
List<Listener> listeners = List.of(
|
||||||
|
new WorldSelectListener(),
|
||||||
|
new CancelListener()
|
||||||
|
);
|
||||||
|
listeners.forEach(listener -> Bukkit.getPluginManager().registerEvents(listener, this));
|
||||||
|
|
||||||
|
this.loadWorlds();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDisable() {
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadWorlds() {
|
||||||
|
File worldFolder = new File(getDataFolder().getAbsolutePath() + "/worlds");
|
||||||
|
//noinspection ResultOfMethodCallIgnored
|
||||||
|
worldFolder.mkdirs();
|
||||||
|
Arrays.stream(Objects.requireNonNull(worldFolder.listFiles())).forEach(file -> this.worlds.add(new ViewableWorld(file)));
|
||||||
|
this.worlds.forEach(ViewableWorld::loadWorld);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Main instance() {
|
||||||
|
return Main.instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ViewableWorld> getWorlds() {
|
||||||
|
return worlds;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
package eu.mhsl.minecraft.WorldMuseum;
|
||||||
|
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.util.TriState;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.World;
|
||||||
|
import org.bukkit.WorldCreator;
|
||||||
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.generator.ChunkGenerator;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class ViewableWorld {
|
||||||
|
private final File worldLocation;
|
||||||
|
private World world;
|
||||||
|
|
||||||
|
private boolean enabled = true;
|
||||||
|
private String name;
|
||||||
|
private Material icon = Material.GRASS_BLOCK;
|
||||||
|
|
||||||
|
public ViewableWorld(File worldLocation) {
|
||||||
|
this.worldLocation = worldLocation;
|
||||||
|
this.name = "unnamed_" + worldLocation.getName();
|
||||||
|
File configFile = new File(this.worldLocation.getAbsolutePath() + "/config.yml");
|
||||||
|
if (configFile.exists()) {
|
||||||
|
ConfigurationSection config = YamlConfiguration.loadConfiguration(configFile);
|
||||||
|
this.enabled = config.getBoolean("enabled");
|
||||||
|
this.name = config.getString("name");
|
||||||
|
this.icon = Material.valueOf(config.getString("icon"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadWorld() {
|
||||||
|
if(world != null) throw new IllegalStateException("World was already loaded");
|
||||||
|
WorldCreator worldCreator = new WorldCreator(this.worldLocation.getAbsolutePath());
|
||||||
|
worldCreator.keepSpawnLoaded(TriState.FALSE);
|
||||||
|
worldCreator.generator(new ChunkGenerator() {});
|
||||||
|
this.world = worldCreator.createWorld();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addViewer(Player player) {
|
||||||
|
player.teleport(world.getSpawnLocation());
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemStack getItem() {
|
||||||
|
ItemStack stack = new ItemStack(this.icon);
|
||||||
|
ItemMeta meta = stack.getItemMeta();
|
||||||
|
meta.displayName(Component.text(name));
|
||||||
|
stack.setItemMeta(meta);
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package eu.mhsl.minecraft.WorldMuseum.worldSelector;
|
||||||
|
|
||||||
|
import eu.mhsl.minecraft.WorldMuseum.Main;
|
||||||
|
import eu.mhsl.minecraft.WorldMuseum.ViewableWorld;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.inventory.Inventory;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class WorldSelectInventory {
|
||||||
|
public static Inventory generateInventory() {
|
||||||
|
List<ViewableWorld> worlds = Main.instance().getWorlds();
|
||||||
|
Inventory inventory = Bukkit.createInventory(null, Math.max(worlds.size() / 9, 1) * 9);
|
||||||
|
|
||||||
|
worlds.stream()
|
||||||
|
.filter(ViewableWorld::isEnabled)
|
||||||
|
.forEach(viewableWorld -> inventory.addItem(viewableWorld.getItem()));
|
||||||
|
|
||||||
|
return inventory;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package eu.mhsl.minecraft.WorldMuseum.worldSelector;
|
||||||
|
|
||||||
|
import eu.mhsl.minecraft.WorldMuseum.Main;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||||
|
import org.bukkit.event.player.PlayerJoinEvent;
|
||||||
|
|
||||||
|
public class WorldSelectListener implements Listener {
|
||||||
|
@EventHandler
|
||||||
|
public void onInteract(InventoryClickEvent event) {
|
||||||
|
event.setCancelled(true);
|
||||||
|
|
||||||
|
Main.instance().getWorlds().stream()
|
||||||
|
.filter(viewableWorld -> viewableWorld.getItem().equals(event.getCurrentItem()))
|
||||||
|
.findFirst()
|
||||||
|
.ifPresent(viewableWorld -> viewableWorld.addViewer((Player) event.getWhoClicked()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onJoin(PlayerJoinEvent event) {
|
||||||
|
Bukkit.getScheduler().scheduleSyncDelayedTask(
|
||||||
|
Main.instance(),
|
||||||
|
() -> event.getPlayer().openInventory(
|
||||||
|
WorldSelectInventory.generateInventory()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +0,0 @@
|
|||||||
package org.example;
|
|
||||||
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
|
||||||
|
|
||||||
public class WorldMuseum extends JavaPlugin {
|
|
||||||
@Override
|
|
||||||
public void onEnable() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDisable() {
|
|
||||||
}
|
|
||||||
}
|
|
3
src/main/resources/config.yml
Normal file
3
src/main/resources/config.yml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
bungee:
|
||||||
|
enabled: true
|
||||||
|
returnToServer: 'server'
|
6
src/main/resources/plugin.yml
Normal file
6
src/main/resources/plugin.yml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
name: WorldMuseum
|
||||||
|
version: 0.1
|
||||||
|
main: eu.mhsl.minecraft.WorldMuseum.Main
|
||||||
|
description: Weltenmuseum
|
||||||
|
author: MineTec
|
||||||
|
api-version: '1.21.1'
|
Loading…
x
Reference in New Issue
Block a user