From 9420b8c5ef71dee01d7e19160992c34d5feef154 Mon Sep 17 00:00:00 2001 From: Leonard Kugis Date: Mon, 25 Apr 2022 18:40:22 +0200 Subject: . --- .../com/encrox/instanceddungeons/Commander.java | 40 ++ .../com/encrox/instanceddungeons/Direction.java | 10 + .../java/com/encrox/instanceddungeons/Dungeon.java | 48 ++ .../com/encrox/instanceddungeons/DungeonWorld.java | 59 +++ .../java/com/encrox/instanceddungeons/Exit.java | 23 + .../instanceddungeons/InstancedDungeons.java | 262 +++++++++++ .../com/encrox/instanceddungeons/Schematic.java | 90 ++++ .../java/com/encrox/instanceddungeons/Section.java | 499 +++++++++++++++++++++ 8 files changed, 1031 insertions(+) create mode 100644 src/main/java/com/encrox/instanceddungeons/Commander.java create mode 100644 src/main/java/com/encrox/instanceddungeons/Direction.java create mode 100644 src/main/java/com/encrox/instanceddungeons/Dungeon.java create mode 100644 src/main/java/com/encrox/instanceddungeons/DungeonWorld.java create mode 100644 src/main/java/com/encrox/instanceddungeons/Exit.java create mode 100644 src/main/java/com/encrox/instanceddungeons/InstancedDungeons.java create mode 100644 src/main/java/com/encrox/instanceddungeons/Schematic.java create mode 100644 src/main/java/com/encrox/instanceddungeons/Section.java (limited to 'src/main/java/com/encrox/instanceddungeons') diff --git a/src/main/java/com/encrox/instanceddungeons/Commander.java b/src/main/java/com/encrox/instanceddungeons/Commander.java new file mode 100644 index 0000000..a9b5f39 --- /dev/null +++ b/src/main/java/com/encrox/instanceddungeons/Commander.java @@ -0,0 +1,40 @@ +package com.encrox.instanceddungeons; + +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.json.JSONObject; + +public class Commander implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if(sender instanceof Player) { + Player player = (Player)sender; + try { + switch(args[0]) { + case "open": + break; + case "invite": + break; + case "join": + break; + case "close": + break; + case "random": + Dungeon dungeon = new Dungeon(player, args[1], Integer.parseInt(args[2])); + InstancedDungeons.dungeons.add(dungeon); + return true; + } + } catch(Exception e) { + e.printStackTrace(); + } + } else { + sender.sendMessage("no player"); + } + return false; + } + +} diff --git a/src/main/java/com/encrox/instanceddungeons/Direction.java b/src/main/java/com/encrox/instanceddungeons/Direction.java new file mode 100644 index 0000000..0b360d1 --- /dev/null +++ b/src/main/java/com/encrox/instanceddungeons/Direction.java @@ -0,0 +1,10 @@ +package com.encrox.instanceddungeons; + +public class Direction { + + public static final byte NORTH = 0; + public static final byte EAST = 1; + public static final byte SOUTH = 2; + public static final byte WEST = 3; + +} diff --git a/src/main/java/com/encrox/instanceddungeons/Dungeon.java b/src/main/java/com/encrox/instanceddungeons/Dungeon.java new file mode 100644 index 0000000..a94ba36 --- /dev/null +++ b/src/main/java/com/encrox/instanceddungeons/Dungeon.java @@ -0,0 +1,48 @@ +package com.encrox.instanceddungeons; + +import java.util.ArrayList; + +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.json.JSONObject; + +import com.sk89q.worldedit.BlockVector; + +public class Dungeon { + + private Section section; + private String id; + private int depth; + private ArrayList players; + + public Dungeon(Player player, String id, int depth) { + this.id = id; + this.depth = depth; + section = new Section(InstancedDungeons.startDescriptors.getJSONObject((int)Math.round(Math.random()*(InstancedDungeons.startDescriptors.length()-1))), 0, depth); + try { + section.load(); + section.instantiate(); + BlockVector[] destinations = section.getAbsoluteExits(); + BlockVector destination = destinations[(int)Math.round(Math.random()*(destinations.length-1))]; + addPlayer(player); + player.teleport(new Location(InstancedDungeons.dungeonWorld.getWorld(), destination.getBlockX(), destination.getBlockY(), destination.getBlockZ())); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void addPlayer(Player player) { + section.addPlayer(player); + } + + public void removePlayer(Player player) { + section.removePlayer(player); + } + + public String getId() { + return id; + } + +} diff --git a/src/main/java/com/encrox/instanceddungeons/DungeonWorld.java b/src/main/java/com/encrox/instanceddungeons/DungeonWorld.java new file mode 100644 index 0000000..7d4b99c --- /dev/null +++ b/src/main/java/com/encrox/instanceddungeons/DungeonWorld.java @@ -0,0 +1,59 @@ +package com.encrox.instanceddungeons; + +import java.util.ArrayList; + +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.plugin.Plugin; + +import com.encrox.instancedregions.InstancedProtectedCuboidRegion; +import com.sk89q.worldedit.BlockVector; +import com.sk89q.worldedit.BlockVector2D; +import com.sk89q.worldedit.regions.CuboidRegion; +import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; + +public class DungeonWorld { + + private Plugin plugin; + private World world; + private ArrayList regions; + private int lastX; + + public DungeonWorld(Plugin plugin, World world) { + this.world = world; + this.plugin = plugin; + regions = new ArrayList(); + lastX = 0; + } + + public InstancedProtectedCuboidRegion allocate(int width, int length) { + BlockVector min, max; + InstancedProtectedCuboidRegion region; + for(int i = 0; true; i+=16) { + if(new ProtectedCuboidRegion("current", (min = new BlockVector(lastX+i, 0, 0)), (max = new BlockVector(lastX+i+width, 255, length))).getIntersectingRegions(regions).isEmpty()) { + region = new InstancedProtectedCuboidRegion(plugin, world, ""+min.getBlockX(), min, max); + regions.add(region); + return region; + } + } + } + + //YOU STILL HAVE TO DISPOSE THE INSTANCED REGION MANUALLY! + public void deallocate(InstancedProtectedCuboidRegion region) { + BlockVector min = region.getMinimumPoint(), max = region.getMaximumPoint(); + for(int y = min.getBlockY(), ymax = max.getBlockY(); y dungeons; + public static WorldEditPlugin we; + + public void onEnable() { + pdf = getDescription(); + logger = Logger.getLogger("Minecraft"); + if(setupMyself() && setupWorldEdit()) { + plugin = this; + getCommand("dungeon").setExecutor(new Commander()); + logger.info(pdf.getName() + " " + pdf.getVersion() + " has been enabled."); + } else { + logger.info(pdf.getName() + " " + pdf.getVersion() + " has been disabled."); + } + } + + public boolean setupWorldEdit() { + we = (WorldEditPlugin)Bukkit.getPluginManager().getPlugin("WorldEdit"); + return (we != null); + } + + public boolean setupMyself() { + if(!this.getDataFolder().exists()) + this.getDataFolder().mkdirs(); + schematicsDirectory = new File(this.getDataFolder(), "schematics"); + if(!schematicsDirectory.exists()) + schematicsDirectory.mkdirs(); + File configFile = new File(this.getDataFolder(), "config.json"); + if(!configFile.exists()) { + BufferedInputStream bis; + FileOutputStream out; + try { + bis = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("config.json")); + out = new FileOutputStream(configFile); + int current; + while((current = bis.read()) != -1) { + out.write(current); + } + bis.close(); + out.close(); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + BufferedReader br; + StringBuilder sb = new StringBuilder(); + String line; + try { + br = new BufferedReader(new FileReader(configFile)); + while((line = br.readLine()) != null) + sb.append(line); + br.close(); + } catch (Exception e1) { + e1.printStackTrace(); + return false; + } + config = new JSONObject(sb.toString()); + resetDungeons = config.getJSONArray("resetDungeons"); + File schematicDescriptorFile = new File(schematicsDirectory, "descriptor.json"); + if(!schematicDescriptorFile.exists()) { + int current; + try { + BufferedInputStream bis = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("descriptor.json")); + FileOutputStream out = new FileOutputStream(schematicDescriptorFile); + while((current = bis.read()) != -1) { + out.write(current); + } + bis.close(); + out.close(); + } catch(Exception e) { + e.printStackTrace(); + return false; + } + try { + BufferedInputStream bis = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("schematics/endBottom.schematic")); + FileOutputStream out = new FileOutputStream(new File(schematicsDirectory, "endBottom.schematic")); + while((current = bis.read()) != -1) { + out.write(current); + } + bis.close(); + out.close(); + } catch(Exception e) { + e.printStackTrace(); + return false; + } + try { + BufferedInputStream bis = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("schematics/endSide.schematic")); + FileOutputStream out = new FileOutputStream(new File(schematicsDirectory, "endSide.schematic")); + while((current = bis.read()) != -1) { + out.write(current); + } + bis.close(); + out.close(); + } catch(Exception e) { + e.printStackTrace(); + return false; + } + try { + BufferedInputStream bis = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("schematics/endTop.schematic")); + FileOutputStream out = new FileOutputStream(new File(schematicsDirectory, "endTop.schematic")); + while((current = bis.read()) != -1) { + out.write(current); + } + bis.close(); + out.close(); + } catch(Exception e) { + e.printStackTrace(); + return false; + } + try { + BufferedInputStream bis = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("schematics/startBottom.schematic")); + FileOutputStream out = new FileOutputStream(new File(schematicsDirectory, "startBottom.schematic")); + while((current = bis.read()) != -1) { + out.write(current); + } + bis.close(); + out.close(); + } catch(Exception e) { + e.printStackTrace(); + return false; + } + try { + BufferedInputStream bis = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("schematics/startSide.schematic")); + FileOutputStream out = new FileOutputStream(new File(schematicsDirectory, "startSide.schematic")); + while((current = bis.read()) != -1) { + out.write(current); + } + bis.close(); + out.close(); + } catch(Exception e) { + e.printStackTrace(); + return false; + } + try { + BufferedInputStream bis = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("schematics/startTop.schematic")); + FileOutputStream out = new FileOutputStream(new File(schematicsDirectory, "startTop.schematic")); + while((current = bis.read()) != -1) { + out.write(current); + } + bis.close(); + out.close(); + } catch(Exception e) { + e.printStackTrace(); + return false; + } + try { + BufferedInputStream bis = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("schematics/test.schematic")); + FileOutputStream out = new FileOutputStream(new File(schematicsDirectory, "test.schematic")); + while((current = bis.read()) != -1) { + out.write(current); + } + bis.close(); + out.close(); + } catch(Exception e) { + e.printStackTrace(); + return false; + } + } + sb = new StringBuilder(); + try { + br = new BufferedReader(new FileReader(schematicDescriptorFile)); + while((line = br.readLine()) != null) + sb.append(line); + br.close(); + } catch (Exception e1) { + e1.printStackTrace(); + return false; + } + schematicDescriptors = new JSONObject(sb.toString()).getJSONArray("schematics"); + startDescriptors = new JSONArray(); + endDescriptors = new JSONArray(); + normalDescriptors = new JSONArray(); + JSONObject current; + for(int i = 0, length = schematicDescriptors.length(); i(); + return true; + } + + public void onDisable() { + logger.info(pdf.getName() + " " + pdf.getVersion() + " has been disabled."); + } + + private void addClassPath(final URL url) throws IOException { + final URLClassLoader sysloader = (URLClassLoader) ClassLoader + .getSystemClassLoader(); + final Class sysclass = URLClassLoader.class; + try { + final Method method = sysclass.getDeclaredMethod("addURL", + new Class[] { URL.class }); + method.setAccessible(true); + method.invoke(sysloader, new Object[] { url }); + } catch (final Throwable t) { + t.printStackTrace(); + throw new IOException("Error adding " + url + + " to system classloader"); + } + } + +} diff --git a/src/main/java/com/encrox/instanceddungeons/Schematic.java b/src/main/java/com/encrox/instanceddungeons/Schematic.java new file mode 100644 index 0000000..c5aaf9e --- /dev/null +++ b/src/main/java/com/encrox/instanceddungeons/Schematic.java @@ -0,0 +1,90 @@ +/* + * Dynamic Schematic object + * + * Currently not supporting entities. + */ + +package com.encrox.instanceddungeons; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import org.bukkit.Material; +import org.jnbt.ByteArrayTag; +import org.jnbt.CompoundTag; +import org.jnbt.ListTag; +import org.jnbt.NBTInputStream; +import org.jnbt.ShortTag; +import org.jnbt.StringTag; +import org.jnbt.Tag; + +public class Schematic { + + public int width, height, length, readIndex, writeIndex; + public String materials; + public byte[] blocks, data; + public List entities, tileEntities; + public File file; + + public Schematic(int width, int height, int length, String materials, byte[] blocks, byte[] data, List entities, List tileEntities) { + this.width = width; + this.height = height; + this.length = length; + this.materials = materials; + this.blocks = blocks; + this.data = data; + this.entities = entities; + this.tileEntities = tileEntities; + readIndex = -1; + writeIndex = -1; + } + + public Schematic(File file) { + this.file = file; + CompoundTag schematicTag = null; + try { + NBTInputStream in = new NBTInputStream(new FileInputStream(file)); + schematicTag = (CompoundTag)in.readTag(); + in.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Map schematic = schematicTag.getValue(); + width = ((ShortTag)schematic.get("Width")).getValue(); + height = ((ShortTag)schematic.get("Height")).getValue(); + length = ((ShortTag)schematic.get("Length")).getValue(); + materials = ((StringTag)schematic.get("Materials")).getValue(); + blocks = ((ByteArrayTag)schematic.get("Blocks")).getValue(); + data = ((ByteArrayTag)schematic.get("Data")).getValue(); + entities = ((ListTag)schematic.get("Entities")).getValue(); + tileEntities = ((ListTag)schematic.get("TileEntities")).getValue(); + InstancedDungeons.logger.info("blocks: " + blocks.length); + InstancedDungeons.logger.info("data: " + data.length); + } + + public byte getBlockIdAt(int x, int y, int z) { + return blocks[(y*length + z)*width + x]; + } + + public byte getNextBlock() { + return blocks[readIndex++]; + } + + public void setBlockIdAt(int x, int y, int z, byte blockId) { + blocks[(y*length + z)*width + x] = blockId; + } + + public void setNextBlock(byte blockId) { + blocks[writeIndex++] = blockId; + } + + public void write(File file) { + + } + +} diff --git a/src/main/java/com/encrox/instanceddungeons/Section.java b/src/main/java/com/encrox/instanceddungeons/Section.java new file mode 100644 index 0000000..282a554 --- /dev/null +++ b/src/main/java/com/encrox/instanceddungeons/Section.java @@ -0,0 +1,499 @@ +package com.encrox.instanceddungeons; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.HandlerList; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.plugin.Plugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.encrox.instancedregions.InstancedProtectedCuboidRegion; +import com.sk89q.worldedit.BlockVector; +import com.sk89q.worldedit.CuboidClipboard; +import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.MaxChangedBlocksException; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.bukkit.BukkitWorld; +import com.sk89q.worldedit.data.DataException; +import com.sk89q.worldedit.schematic.MCEditSchematicFormat; + +public class Section { + + private Schematic schematic; + private String schematicFileName; + private Map exitMap; + private ArrayList players; + private BlockVector[] exits; + private BlockVector size, nearestPositiveX, nearestNegativeX, nearestPositiveY, nearestNegativeY, nearestPositiveZ, nearestNegativeZ; + private int index, depth; + private InstancedProtectedCuboidRegion region; + private Listener trigger; + private volatile boolean justEntered, portal = false; + private JSONObject descriptor; + + public Section(JSONObject descriptor, int index, int depth) { + this.index = index; + this.depth = depth; + players = new ArrayList(); + exitMap = new HashMap(); + schematicFileName = descriptor.getString("file"); + JSONArray size = descriptor.getJSONArray("size"); + this.size = new BlockVector(size.getInt(0), size.getInt(1), size.getInt(2)); + JSONArray exits = descriptor.getJSONArray("exits"); + this.exits = new BlockVector[exits.length()]; + for(int i = 0; i usable = new ArrayList(); + if(exits[i].getBlockX() == 0.0 || exits[i].getBlockX() == size.getBlockX()-1 || exits[i].getBlockZ() == 0.0 || exits[i].getBlockZ() == size.getBlockZ()-1) { + if(index == depth) { + for(int c = 0, length = InstancedDungeons.endDescriptors.length(); c getSideExits() { + ArrayList out = new ArrayList(); + for(int i = 0; i getTopExits() { + ArrayList out = new ArrayList(); + for(int i = 0; i getBottomExits() { + ArrayList out = new ArrayList(); + for(int i = 0; i dist) { + dist = current; + nearest = exits[i]; + } + } + } + return nearest; + } + + public BlockVector getNearestExitNegativeX(org.bukkit.util.Vector relative) { + BlockVector nearest = null; + double dist = 0.0, current; + for(int i = 0; i dist) { + dist = current; + nearest = exits[i]; + } + } + } + return nearest; + } + + public BlockVector getNearestExitPositiveY(org.bukkit.util.Vector relative) { + BlockVector nearest = null; + double dist = 0.0, current; + for(int i = 0; i dist) { + dist = current; + nearest = exits[i]; + } + } + } + return nearest; + } + + public BlockVector getNearestExitNegativeY(org.bukkit.util.Vector relative) { + BlockVector nearest = null; + double dist = 0.0, current; + for(int i = 0; i dist) { + dist = current; + nearest = exits[i]; + } + } + } + return nearest; + } + + public BlockVector getNearestExitPositiveZ(org.bukkit.util.Vector relative) { + BlockVector nearest = null; + double dist = 0.0, current; + for(int i = 0; i dist) { + dist = current; + nearest = exits[i]; + } + } + } + return nearest; + } + + public BlockVector getNearestExitNegativeZ(org.bukkit.util.Vector relative) { + BlockVector nearest = null; + double dist = 0.0, current; + for(int i = 0; i dist) { + dist = current; + nearest = exits[i]; + } + } + } + return nearest; + } + + public void instantiate() { + InstancedDungeons.logger.info("Instantiating section (file: " + descriptor.getString("file") + ", index: " + index + ", depth: " + depth + ")"); + justEntered = true; + portal = false; + region = InstancedDungeons.dungeonWorld.allocate(size.getBlockX(), size.getBlockZ()); + Iterator iter = players.iterator(); + while(iter.hasNext()) + region.addPlayer(iter.next()); + //write schematic to map + final BlockVector min = region.getMinimumPoint(); + + for(int i = 0; i exits = exitMap.values().iterator(); + while(exits.hasNext()) + exits.next().getSection().addPlayer(player); + } + } + + public void removePlayer(Player player) { + players.add(player); + if(region != null) + region.removePlayer(player); + if(exitMap != null) { + Iterator exits = exitMap.values().iterator(); + while(exits.hasNext()) + exits.next().getSection().removePlayer(player); + } + } + +} -- cgit v1.2.1