Solved Saving a region to 3D array and then file

Discussion in 'Plugin Help/Development/Requests' started by benthomas7777, Jun 11, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    benthomas7777

    Hi, I am trying to save a 11 by 11 by 11 area of the world to a 3d array, but have a lot of areas. I want it so that when you place a piece of bedrock that it saves the area array it. The bedrock is the center, bottom block of the region.

    Includes extra code for deleting the region, creating a custom block that when place creates the saved region in the world.

    Console Error Code (open)

    Code:
    E:\eclipse\Test Server>java -Xmx1024M -jar bukkit.jar -o true
    Loading libraries, please wait...
    [16:16:08 INFO]: Starting minecraft server version 1.8.3
    [16:16:08 INFO]: Loading properties
    [16:16:08 INFO]: Default game type: SURVIVAL
    [16:16:08 INFO]: Generating keypair
    [16:16:08 INFO]: Starting Minecraft server on *:25565
    [16:16:08 INFO]: Using default channel type
    [16:16:09 INFO]: This server is running CraftBukkit version git-Bukkit-6e0120a (
    MC: 1.8.3) (Implementing API version 1.8.3-R0.1-SNAPSHOT)
    [16:16:09 INFO]: [AgeofEmpires] Loading AgeofEmpires v1.0
    [16:16:09 INFO]: Preparing level "world"
    [16:16:09 INFO]: Preparing start region for level 0 (Seed: -3064742164155234582)
    
    [16:16:10 INFO]: Preparing start region for level 1 (Seed: 6883102785784422186)
    [16:16:11 INFO]: Preparing spawn area: 83%
    [16:16:11 INFO]: Preparing start region for level 2 (Seed: 6883102785784422186)
    [16:16:11 INFO]: [AgeofEmpires] Enabling AgeofEmpires v1.0
    [16:16:11 INFO]: [AgeofEmpires] Plugin Started
    [16:16:11 INFO]: Server permissions file permissions.yml is empty, ignoring it
    [16:16:11 INFO]: Done (2.560s)! For help, type "help" or "?"
    [16:16:17 INFO]: UUID of player benthomas7777 is 9574d196-e777-4f8f-a99f-5af393d
    02a43
    [16:16:17 INFO]: benthomas7777[/127.0.0.1:53554] logged in with entity id 109 at
    ([world]385.285999411139, 5.0, 2455.5876085438294)
    [16:16:23 ERROR]: Could not pass event BlockPlaceEvent to AgeofEmpires v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:297) ~[bukkit.jar:git-Bukkit-6e0120a]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[bukkit.jar:git-Bukkit-6e0120a]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [bukkit.jar:git-Bukkit-6e0120a]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [bukkit.jar:git-Bukkit-6e0120a]
            at org.bukkit.craftbukkit.v1_8_R2.event.CraftEventFactory.callBlockPlace
    Event(CraftEventFactory.java:124) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.ItemStack.placeItem(ItemStack.java:156)
    [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.PlayerInteractManager.interact(PlayerInt
    eractManager.java:502) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java
    :703) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.PacketPlayInBlockPlace.a(PacketPlayInBlo
    ckPlace.java:52) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.PacketPlayInBlockPlace.a(PacketPlayInBlo
    ckPlace.java:1) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:1
    3) [bukkit.jar:git-Bukkit-6e0120a]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:51
    1) [?:1.8.0_40]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_40]
    
            at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [bukkit.jar
    :git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:6
    70) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    36) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    26) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :534) [bukkit.jar:git-Bukkit-6e0120a]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_40]
    Caused by: java.lang.NullPointerException
            at me.benthomas7777.ageofempires.Main.onBlockPlace(Main.java:63) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _40]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
    java:62) ~[?:1.8.0_40]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
    sorImpl.java:43) ~[?:1.8.0_40]
            at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_40]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:295) ~[bukkit.jar:git-Bukkit-6e0120a]
            ... 18 more
    [16:16:24 ERROR]: Could not pass event BlockPlaceEvent to AgeofEmpires v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:297) ~[bukkit.jar:git-Bukkit-6e0120a]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[bukkit.jar:git-Bukkit-6e0120a]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [bukkit.jar:git-Bukkit-6e0120a]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [bukkit.jar:git-Bukkit-6e0120a]
            at org.bukkit.craftbukkit.v1_8_R2.event.CraftEventFactory.callBlockPlace
    Event(CraftEventFactory.java:124) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.ItemStack.placeItem(ItemStack.java:156)
    [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.PlayerInteractManager.interact(PlayerInt
    eractManager.java:502) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java
    :703) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.PacketPlayInBlockPlace.a(PacketPlayInBlo
    ckPlace.java:52) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.PacketPlayInBlockPlace.a(PacketPlayInBlo
    ckPlace.java:1) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:1
    3) [bukkit.jar:git-Bukkit-6e0120a]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:51
    1) [?:1.8.0_40]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_40]
    
            at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [bukkit.jar
    :git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:6
    70) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    36) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    26) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :534) [bukkit.jar:git-Bukkit-6e0120a]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_40]
    Caused by: java.lang.NullPointerException
            at me.benthomas7777.ageofempires.Main.onBlockPlace(Main.java:63) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _40]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
    java:62) ~[?:1.8.0_40]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
    sorImpl.java:43) ~[?:1.8.0_40]
            at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_40]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:295) ~[bukkit.jar:git-Bukkit-6e0120a]
            ... 18 more
    [16:16:28 ERROR]: Could not pass event BlockPlaceEvent to AgeofEmpires v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:297) ~[bukkit.jar:git-Bukkit-6e0120a]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[bukkit.jar:git-Bukkit-6e0120a]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [bukkit.jar:git-Bukkit-6e0120a]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [bukkit.jar:git-Bukkit-6e0120a]
            at org.bukkit.craftbukkit.v1_8_R2.event.CraftEventFactory.callBlockPlace
    Event(CraftEventFactory.java:124) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.ItemStack.placeItem(ItemStack.java:156)
    [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.PlayerInteractManager.interact(PlayerInt
    eractManager.java:502) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java
    :703) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.PacketPlayInBlockPlace.a(PacketPlayInBlo
    ckPlace.java:52) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.PacketPlayInBlockPlace.a(PacketPlayInBlo
    ckPlace.java:1) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:1
    3) [bukkit.jar:git-Bukkit-6e0120a]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:51
    1) [?:1.8.0_40]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_40]
    
            at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [bukkit.jar
    :git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:6
    70) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:3
    36) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:6
    26) [bukkit.jar:git-Bukkit-6e0120a]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java
    :534) [bukkit.jar:git-Bukkit-6e0120a]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_40]
    Caused by: java.lang.NullPointerException
            at me.benthomas7777.ageofempires.Main.onBlockPlace(Main.java:128) ~[?:?]
    
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _40]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
    java:62) ~[?:1.8.0_40]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
    sorImpl.java:43) ~[?:1.8.0_40]
            at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_40]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:295) ~[bukkit.jar:git-Bukkit-6e0120a]
            ... 18 more
    >


    Code :
    Code (open)

    Code:
    package me.benthomas7777.ageofempires;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    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.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
        String[][][] Wall;
    
        public void onEnable() {
            getLogger().info("Plugin Started");
            getServer().getPluginManager().registerEvents(this, this);
    
            File dir = getDataFolder();
    
            if (!dir.exists())
                if (!dir.mkdir())
                    System.out.println("Could not create directory.");
            Wall = (String[][][]) load(new File(getDataFolder(), "wall.dat"));
    
            ItemStack wall = new ItemStack(Material.SEA_LANTERN, 1);
            ItemMeta iwall = wall.getItemMeta();
            iwall.setDisplayName(ChatColor.RESET + "Wood Wall");
            wall.setItemMeta(iwall);
            ShapedRecipe rwall = new ShapedRecipe(wall);
            rwall.shape("psp", "fff", "www");
            rwall.setIngredient('w', Material.LOG);
            rwall.setIngredient('f', Material.FENCE);
            rwall.setIngredient('p', Material.WOOD);
            rwall.setIngredient('s', Material.WOOD_STEP);
            getServer().addRecipe(rwall);
    
        }
    
        public void onDisable() {
            save(Wall, new File(getDataFolder(), "wall.dat"));
        }
    
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent event) {
            if (event.getBlock().getType() == Material.SEA_LANTERN) {
                if (event.getPlayer().getItemInHand().hasItemMeta()) {
                    if (event.getPlayer().getItemInHand().getItemMeta()
                            .getDisplayName().toString().toLowerCase()
                            .contains("wood wall")) {
                        Block b = event.getBlock();
                        for (int x = -5; x < 6; x++) {
                            for (int z = -5; z < 6; z++) {
                                for (int y = 0; y < 11; y++) {
                                    if (Wall[x][y][z].equals("w")) {
                                        b.getLocation().add(x, y, z).getBlock()
                                                .setType(Material.LOG);
                                    } else {
                                        if (Wall[x][y][z].equals("p")) {
                                            b.getLocation().add(x, y, z).getBlock()
                                                    .setType(Material.WOOD);
                                        } else {
                                            if (Wall[x][y][z].equals("f")) {
                                                b.getLocation().add(x, y, z)
                                                        .getBlock()
                                                        .setType(Material.FENCE);
                                            } else {
                                                if (Wall[x][y][z].equals("s")) {
                                                    b.getLocation()
                                                            .add(x, y, z)
                                                            .getBlock()
                                                            .setType(
                                                                    Material.WOOD_STEP);
                                                } else {
                                                    if (Wall[x][y][z].equals("a")) {
                                                        b.getLocation()
                                                                .add(x, y, z)
                                                                .getBlock()
                                                                .setType(
                                                                        Material.AIR);
                                                    }
                                                }
    
                                            }
                                        }
                                    }
    
                                }
                            }
    
                        }
                    }
                }
            }
            if (event.getBlock().getType() == Material.BEDROCK) {
                Block b = event.getBlock();
                for (int x = -5; x < 6; x++) {
                    for (int z = -5; z < 6; z++) {
                        for (int y = 0; y < 11; y++) {
                            if (b.getLocation().add(x, y, z).getBlock().getType()
                                    .equals(Material.LOG)) {
                                Wall[x][y][z] = "w";
                            } else {
                                if (b.getLocation().add(x, y, z).getBlock()
                                        .getType().equals(Material.WOOD)) {
                                    Wall[x][y][z] = "p";
                                } else {
                                    if (b.getLocation().add(x, y, z).getBlock()
                                            .getType().equals(Material.FENCE)) {
                                        Wall[x][y][z] = "f";
                                    } else {
                                        if (b.getLocation().add(x, y, z).getBlock()
                                                .getType()
                                                .equals(Material.WOOD_STEP)) {
                                            Wall[x][y][z] = "s";
                                        } else {
                                            if (b.getLocation().add(x, y, z)
                                                    .getBlock().getType()
                                                    .equals(Material.AIR)) {
                                                Wall[x][y][z] = "a";
                                            }
    
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
    
        }
    
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            if (event.getBlock().getType() == Material.SEA_LANTERN) {
                Block b = event.getBlock();
                for (int x = -5; x < 6; x++) {
                    for (int z = -5; z < 6; z++) {
                        for (int y = 0; y < 11; y++) {
                            b.getLocation().add(x, y, z).getBlock()
                                    .setType(Material.AIR);
    
                        }
                    }
                }
            }
        }
    
        public void save(Object o, File f) {
            try {
                if (!f.exists())
                    f.createNewFile();
                ObjectOutputStream oos = new ObjectOutputStream(
                        new FileOutputStream(f));
                oos.writeObject(o);
                oos.flush();
                oos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public Object load(File f) {
            try {
                ObjectInputStream ois = new ObjectInputStream(
                        new FileInputStream(f));
                Object result = ois.readObject();
                ois.close();
                return result;
            } catch (Exception e) {
                return null;
            }
        }
    
    }
    
     
Thread Status:
Not open for further replies.

Share This Page