From 19b349962a6d7e3a0e3cd2041df196693a6fb187 Mon Sep 17 00:00:00 2001 From: ThePixelbrain <19214217+ThePixelbrain@users.noreply.github.com> Date: Fri, 16 Jun 2023 15:33:32 +0200 Subject: [PATCH] Rewritten and improved data structure: - Islands have owner property - Multi Dimension support - Islands spawn in the center of each region file --- CODEOWNERS | 3 - LICENSE-template | 21 -- .../com/cricketcraft/ftbisland/Config.java | 91 +++++++ .../cricketcraft/ftbisland/FTBIslands.java | 213 +---------------- .../cricketcraft/ftbisland/IslandCreator.java | 226 +++++++----------- .../cricketcraft/ftbisland/IslandStorage.java | 55 +++++ .../cricketcraft/ftbisland/IslandUtils.java | 152 +++++++----- .../commands/CreateIslandCommand.java | 13 +- .../commands/DeleteIslandCommand.java | 20 +- .../ftbisland/commands/JoinIslandCommand.java | 41 +--- .../ftbisland/commands/ListIslandCommand.java | 11 +- .../commands/RenameIslandCommand.java | 25 +- .../commands/SetIslandSpawnCommand.java | 57 ----- .../commands/TeleportIslandCommand.java | 6 +- .../cricketcraft/ftbisland/model/Island.java | 64 +++++ .../ftbisland/model/IslandContainer.java | 58 +++++ 16 files changed, 500 insertions(+), 556 deletions(-) delete mode 100644 CODEOWNERS delete mode 100644 LICENSE-template create mode 100644 src/main/java/com/cricketcraft/ftbisland/Config.java create mode 100644 src/main/java/com/cricketcraft/ftbisland/IslandStorage.java delete mode 100644 src/main/java/com/cricketcraft/ftbisland/commands/SetIslandSpawnCommand.java create mode 100644 src/main/java/com/cricketcraft/ftbisland/model/Island.java create mode 100644 src/main/java/com/cricketcraft/ftbisland/model/IslandContainer.java diff --git a/CODEOWNERS b/CODEOWNERS deleted file mode 100644 index a6b5f68..0000000 --- a/CODEOWNERS +++ /dev/null @@ -1,3 +0,0 @@ -# Any Github changes require admin approval -/.github/** @GTNewHorizons/admin - diff --git a/LICENSE-template b/LICENSE-template deleted file mode 100644 index 242da62..0000000 --- a/LICENSE-template +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/src/main/java/com/cricketcraft/ftbisland/Config.java b/src/main/java/com/cricketcraft/ftbisland/Config.java new file mode 100644 index 0000000..1cae5cf --- /dev/null +++ b/src/main/java/com/cricketcraft/ftbisland/Config.java @@ -0,0 +1,91 @@ +package com.cricketcraft.ftbisland; + +import java.io.File; +import java.util.ArrayList; + +import net.minecraftforge.common.config.Configuration; + +import cpw.mods.fml.client.event.ConfigChangedEvent; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; + +public class Config { + + private static Configuration config; + + public static void init(File file) { + if (config == null) { + config = new Configuration(file); + loadConfig(); + } + } + + private static void loadConfig() { + FTBIslands.maxIslands = config.getInt( + "Max Islands", + "misc", + 100, + 1, + 1000, + "The maximum amount of islands that can be created. This number will be multiplied by four." + + " Be careful with high numbers."); + if (!config.hasKey("misc", "Island Type")) { + boolean skyFactory = config + .getBoolean("Sky Factory", "misc", false, "Set this to true if you are playing on Sky Factory."); + boolean platform = config.getBoolean( + "Platform", + "misc", + false, + "Set to true if you want to start on a 3x3 platform, or false for a tree."); + if (skyFactory || !platform) { + FTBIslands.islandType = config.getString( + "Island Type", + "misc", + "tree", + "Set this to the type of platform you want:\n" + " 'grass' A single grass block.\n" + + " 'tree' A small oak tree on a grass block. This is the standard start.\n" + + " 'platform' A 3x3 platform with a chest.\n" + + " 'GoG' An island similar to Garden of Glass from Botania.\n"); + config.moveProperty("misc", "Sky Factory", "forRemoval"); + config.moveProperty("misc", "Platform", "forRemoval"); + config.removeCategory(config.getCategory("forRemoval")); + } + } else { + FTBIslands.islandType = config.getString( + "Island Type", + "misc", + "tree", + "Set this to the type of platform you want:\n" + " 'grass' A single grass block.\n" + + " 'tree' A small oak tree on a grass block. This is the standard start.\n" + + " 'platform' A 3x3 platform with a chest.\n" + + " 'GoG' An island similar to Garden of Glass from Botania.\n"); + ArrayList types = new ArrayList<>(); + types.add("grass"); + types.add("tree"); + types.add("platform"); + types.add("GoG"); + + boolean valid = false; + for (String s : types) { + if (FTBIslands.islandType.equalsIgnoreCase(s)) { + valid = true; + break; + } + } + if (!valid) { + FTBIslands.logger.warn("Invalid island option detected. Using 'platform' as default."); + FTBIslands.islandType = "platform"; + } + } + + if (config.hasChanged()) { + config.save(); + } + } + + @SubscribeEvent + public void onChanged(ConfigChangedEvent.OnConfigChangedEvent event) { + if (event.modID.equalsIgnoreCase(Tags.MODID)) { + loadConfig(); + } + } +} diff --git a/src/main/java/com/cricketcraft/ftbisland/FTBIslands.java b/src/main/java/com/cricketcraft/ftbisland/FTBIslands.java index 18710b0..c997390 100644 --- a/src/main/java/com/cricketcraft/ftbisland/FTBIslands.java +++ b/src/main/java/com/cricketcraft/ftbisland/FTBIslands.java @@ -1,26 +1,15 @@ package com.cricketcraft.ftbisland; import java.io.*; -import java.util.ArrayList; -import java.util.HashMap; -import net.minecraftforge.common.config.Configuration; - -import org.apache.commons.io.FileUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.cricketcraft.ftbisland.commands.*; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.reflect.TypeToken; -import cpw.mods.fml.client.event.ConfigChangedEvent; import cpw.mods.fml.common.Mod; -import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.event.FMLServerStartingEvent; -import cpw.mods.fml.common.eventhandler.SubscribeEvent; import ftb.lib.LMMod; @Mod( @@ -32,14 +21,12 @@ public class FTBIslands { public static int maxIslands; - public static File islandFile; public static Logger logger; public static String islandType; public static LMMod mod; - private static File oldIslands; private static File directory; - public static ArrayList islandLoc = new ArrayList(); - private static HashMap islands = new HashMap<>(); + + private static IslandStorage islandStorage; @Mod.EventHandler public void serverLoading(FMLServerStartingEvent event) { @@ -49,208 +36,26 @@ public void serverLoading(FMLServerStartingEvent event) { event.registerServerCommand(new JoinIslandCommand()); event.registerServerCommand(new ListIslandCommand()); event.registerServerCommand(new RenameIslandCommand()); - event.registerServerCommand(new SetIslandSpawnCommand()); event.registerServerCommand(new TeleportIslandCommand()); logger.info("Finished registering commands."); - loadIslands(); - loadChestLoot(); - reloadIslands(); - } - - public static HashMap getIslands() { - return islands; - } - - private void loadIslands() { - for (int c = 0; c < maxIslands; c++) { - addIslandToList(c); - } - } - - private void addIslandToList(int x) { - if (x != 0) { - islandLoc.add(new IslandCreator.IslandPos(x * 1000, 60, x * 1000)); - islandLoc.add(new IslandCreator.IslandPos(-x * 1000, 60, x * 1000)); - islandLoc.add(new IslandCreator.IslandPos(-x * 1000, 60, -x * 1000)); - islandLoc.add(new IslandCreator.IslandPos(x * 1000, 60, -x * 1000)); - } else { - islandLoc.add(new IslandCreator.IslandPos(x * 1000, 60, x * 1000)); - } - } - - private void loadChestLoot() { - + islandStorage.reloadContainer(); } @Mod.EventHandler - public void preInit(FMLPreInitializationEvent event) { + public static void preInit(FMLPreInitializationEvent event) { Config.init(new File(event.getModConfigurationDirectory(), "ftbi/FTB_Islands.cfg")); logger = LogManager.getLogger("FTBI"); mod = LMMod.create("FTBI"); File dir = event.getModConfigurationDirectory(); directory = new File(dir.getParentFile(), "local"); - oldIslands = new File(directory, "islands.ser"); - islandFile = new File(directory, "islands.json"); - if (oldIslands.exists()) { - logger.info("Islands.ser found, attempting conversion."); - try { - convert(); - } catch (IOException e) { - e.printStackTrace(); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } - } - try { - directory.mkdirs(); - islandFile.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - } + islandStorage = new IslandStorage(); } - @Mod.EventHandler - public void postInit(FMLPostInitializationEvent event) throws IOException { - BufferedReader br = new BufferedReader(new FileReader(islandFile.getPath())); - if (br.readLine() == null) { - logger.info("Islands file empty, placing a default value."); - FTBIslands.islands.put("default", new IslandCreator.IslandPos(0, 60, 0)); - try { - saveIslands(FTBIslands.islands); - } catch (IOException e) { - e.printStackTrace(); - } - } - br.close(); + public static File getDirectory() { + return directory; } - public static void saveIslands(HashMap map) throws IOException { - String s = new GsonBuilder().create() - .toJson(map); - FileUtils.writeStringToFile(islandFile, s); - } - - public static void reloadIslands() { - try { - islands = FTBIslands.getIslandsFromFile(); - } catch (EOFException e) { - // silent catch - } catch (IOException e) { - FTBIslands.logger.error("Couldn't get islands from save file"); - e.printStackTrace(); - } - } - - private static HashMap getIslandsFromFile() throws IOException { - try (FileInputStream ignored = new FileInputStream(islandFile)) { - return new Gson().fromJson( - FileUtils.readFileToString(islandFile), - new TypeToken>() {}.getType()); - } - } - - private static class Config { - - private static Configuration config; - - public static void init(File file) { - if (config == null) { - config = new Configuration(file); - loadConfig(); - } - } - - private static void loadConfig() { - FTBIslands.maxIslands = config.getInt( - "Max Islands", - "misc", - 100, - 1, - 1000, - "The maximum amount of islands that can be created. This number will be multiplied by four." - + " Be careful with high numbers."); - if (!config.hasKey("misc", "Island Type")) { - boolean skyFactory = config - .getBoolean("Sky Factory", "misc", false, "Set this to true if you are playing on Sky Factory."); - boolean platform = config.getBoolean( - "Platform", - "misc", - false, - "Set to true if you want to start on a 3x3 platform, or false for a tree."); - if (skyFactory || !platform) { - FTBIslands.islandType = config.getString( - "Island Type", - "misc", - "tree", - "Set this to the type of platform you want:\n" + " 'grass' A single grass block.\n" - + " 'tree' A small oak tree on a grass block. This is the standard start.\n" - + " 'platform' A 3x3 platform with a chest.\n" - + " 'GoG' An island similar to Garden of Glass from Botania.\n"); - config.moveProperty("misc", "Sky Factory", "forRemoval"); - config.moveProperty("misc", "Platform", "forRemoval"); - config.removeCategory(config.getCategory("forRemoval")); - } - } else { - FTBIslands.islandType = config.getString( - "Island Type", - "misc", - "tree", - "Set this to the type of platform you want:\n" + " 'grass' A single grass block.\n" - + " 'tree' A small oak tree on a grass block. This is the standard start.\n" - + " 'platform' A 3x3 platform with a chest.\n" - + " 'GoG' An island similar to Garden of Glass from Botania.\n"); - ArrayList types = new ArrayList<>(); - types.add("grass"); - types.add("tree"); - types.add("platform"); - types.add("GoG"); - - boolean valid = false; - for (String s : types) { - if (FTBIslands.islandType.equalsIgnoreCase(s)) { - valid = true; - break; - } - } - if (!valid) { - logger.warn("Invalid island option detected. Using 'platform' as default."); - FTBIslands.islandType = "platform"; - } - } - - if (config.hasChanged()) { - config.save(); - } - } - - @SubscribeEvent - public void onChanged(ConfigChangedEvent.OnConfigChangedEvent event) { - if (event.modID.equalsIgnoreCase(Tags.MODID)) { - loadConfig(); - } - } - } - - private static void convert() throws IOException, ClassNotFoundException { - if (!oldIslands.exists()) { - return; - } - logger.info("Old islands file found! Trying to convert to new format!"); - - FileInputStream fileIn = new FileInputStream(oldIslands); - ObjectInputStream in = new ObjectInputStream(fileIn); - - HashMap map = (HashMap) in.readObject(); - in.close(); - fileIn.close(); - String s = new GsonBuilder().create() - .toJson(map); - - File newFile = new File(directory, "islands.json"); - FileOutputStream outputStream = new FileOutputStream(newFile); - FileUtils.writeStringToFile(newFile, s); - outputStream.close(); - oldIslands.delete(); - logger.info("Conversion completed."); + public static IslandStorage getIslandStorage() { + return islandStorage; } } diff --git a/src/main/java/com/cricketcraft/ftbisland/IslandCreator.java b/src/main/java/com/cricketcraft/ftbisland/IslandCreator.java index ca3f1e0..fee59af 100644 --- a/src/main/java/com/cricketcraft/ftbisland/IslandCreator.java +++ b/src/main/java/com/cricketcraft/ftbisland/IslandCreator.java @@ -1,8 +1,6 @@ package com.cricketcraft.ftbisland; -import java.io.IOException; -import java.io.Serializable; - +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.init.Items; import net.minecraft.item.Item; @@ -12,167 +10,111 @@ import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; +import com.cricketcraft.ftbisland.model.Island; +import com.cricketcraft.ftbisland.model.IslandContainer; + import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.registry.GameRegistry; public class IslandCreator { private static final Item chickenStick = GameRegistry.findItem("excompressum", "chickenStick"); - public final String playerName; - public final IslandPos pos; - public IslandCreator() { - playerName = null; - pos = null; - } - - public IslandCreator(String playerName, IslandPos pos) { - this.playerName = playerName; - this.pos = pos; - } - - public static boolean spawnIslandAt(World world, int x, int y, int z, String islandName) { - FTBIslands.reloadIslands(); - if (!FTBIslands.getIslands() - .containsKey(islandName)) { - if (FTBIslands.islandType.equalsIgnoreCase("tree")) { - world.setBlock(x, y, z, Blocks.grass); - for (int c = -3; c < 2; c++) { - for (int d = -3; d < 2; d++) { - for (int e = 3; e < 5; e++) { - world.setBlock(x + (c) + 1, y + e, d + (z) + 1, Blocks.leaves); - } + public static void spawnIslandAt(World world, int x, int y, int z, String islandName, EntityPlayer owner) { + if (FTBIslands.islandType.equalsIgnoreCase("tree")) { + world.setBlock(x, y, z, Blocks.grass); + for (int c = -3; c < 2; c++) { + for (int d = -3; d < 2; d++) { + for (int e = 3; e < 5; e++) { + world.setBlock(x + (c) + 1, y + e, d + (z) + 1, Blocks.leaves); } } - for (int c = -2; c < 1; c++) { - for (int d = -2; d < 1; d++) { - world.setBlock(x + (c) + 1, y + 5, d + (z) + 1, Blocks.leaves); - } + } + for (int c = -2; c < 1; c++) { + for (int d = -2; d < 1; d++) { + world.setBlock(x + (c) + 1, y + 5, d + (z) + 1, Blocks.leaves); } + } - world.setBlock(x, y + 6, z, Blocks.leaves); - world.setBlock(x + 1, y + 6, z, Blocks.leaves); - world.setBlock(x, y + 6, z + 1, Blocks.leaves); - world.setBlock(x - 1, y + 6, z, Blocks.leaves); - world.setBlock(x, y + 6, z - 1, Blocks.leaves); - world.setBlockToAir(x + 2, y + 4, z + 2); + world.setBlock(x, y + 6, z, Blocks.leaves); + world.setBlock(x + 1, y + 6, z, Blocks.leaves); + world.setBlock(x, y + 6, z + 1, Blocks.leaves); + world.setBlock(x - 1, y + 6, z, Blocks.leaves); + world.setBlock(x, y + 6, z - 1, Blocks.leaves); + world.setBlockToAir(x + 2, y + 4, z + 2); - for (int c = 0; c < 5; c++) { - world.setBlock(x, y + c + 1, z, Blocks.log); - } - } else if (FTBIslands.islandType.equalsIgnoreCase("grass")) { - world.setBlock(x, y, z, Blocks.grass); - world.setBlock(x, y + 1, z, Blocks.standing_sign, 6, 3); - ((TileEntitySign) world.getTileEntity(x, y + 1, z)).signText[0] = "You get it yet?"; - } else if (FTBIslands.islandType.equalsIgnoreCase("GoG")) { - // This is similar to how Botania itself generates an island in GoG. This is being done to avoid a soft - // dependency. - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 4; j++) { - for (int k = 0; k < 3; k++) { - world.setBlock(x - 1 + i, y - j, z - 1 + k, j == 0 ? Blocks.grass : Blocks.dirt); - } + for (int c = 0; c < 5; c++) { + world.setBlock(x, y + c + 1, z, Blocks.log); + } + } else if (FTBIslands.islandType.equalsIgnoreCase("grass")) { + world.setBlock(x, y, z, Blocks.grass); + world.setBlock(x, y + 1, z, Blocks.standing_sign, 6, 3); + ((TileEntitySign) world.getTileEntity(x, y + 1, z)).signText[0] = "You get it yet?"; + } else if (FTBIslands.islandType.equalsIgnoreCase("GoG")) { + // This is similar to how Botania itself generates an island in GoG. This is being done to avoid a soft + // dependency. + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 4; j++) { + for (int k = 0; k < 3; k++) { + world.setBlock(x - 1 + i, y - j, z - 1 + k, j == 0 ? Blocks.grass : Blocks.dirt); } } - world.setBlock(x - 1, y - 1, z, Blocks.flowing_water); - int[][] roots = new int[][] { { -1, -2, -1 }, { -1, -4, -2 }, { -2, -3, -1 }, { -2, -3, -2 }, - { 1, -3, -1 }, { 1, -4, -1 }, { 2, -4, -1 }, { 2, -4, 0 }, { 3, -5, 0 }, { 0, -2, 1 }, { 0, -3, 2 }, - { 0, -4, 3 }, { 1, -4, 3 }, { 1, -5, 2 }, { 1, -2, 0 }, }; - if (Loader.isModLoaded("Botania")) { - world.setBlock(x + 1, y + 3, z + 1, GameRegistry.findBlock("Botania", "manaFlame")); - world.setBlock(x, y - 3, z, Blocks.bedrock); - for (int[] pos : roots) { - world.setBlock(x + pos[0], y + pos[1], z + pos[2], GameRegistry.findBlock("Botania", "root")); - } - } else { - for (int[] pos : roots) { - world.setBlock(x + pos[0], y + pos[1], z + pos[2], Blocks.log, 12, 3); - } + } + world.setBlock(x - 1, y - 1, z, Blocks.flowing_water); + int[][] roots = new int[][] { { -1, -2, -1 }, { -1, -4, -2 }, { -2, -3, -1 }, { -2, -3, -2 }, { 1, -3, -1 }, + { 1, -4, -1 }, { 2, -4, -1 }, { 2, -4, 0 }, { 3, -5, 0 }, { 0, -2, 1 }, { 0, -3, 2 }, { 0, -4, 3 }, + { 1, -4, 3 }, { 1, -5, 2 }, { 1, -2, 0 }, }; + if (Loader.isModLoaded("Botania")) { + world.setBlock(x + 1, y + 3, z + 1, GameRegistry.findBlock("Botania", "manaFlame")); + world.setBlock(x, y - 3, z, Blocks.bedrock); + for (int[] pos : roots) { + world.setBlock(x + pos[0], y + pos[1], z + pos[2], GameRegistry.findBlock("Botania", "root")); } } else { - for (int c = 0; c < 3; c++) { - for (int d = 0; d < 3; d++) { - world.setBlock(x + c, y, z + d, Blocks.dirt); - } - } - - world.setBlock(x + 2, y + 1, z + 1, Blocks.chest); - world.getBlock(x + 2, y + 1, z + 1) - .rotateBlock(world, x + 2, y + 1, z + 1, ForgeDirection.WEST); - TileEntityChest chest = (TileEntityChest) world.getTileEntity(x + 2, y + 1, z + 1); - - chest.setInventorySlotContents(0, new ItemStack(Blocks.flowing_water, 1)); - chest.setInventorySlotContents(1, new ItemStack(Blocks.flowing_lava, 1)); - chest.setInventorySlotContents(2, new ItemStack(Items.dye, 64, 15)); - chest.setInventorySlotContents(3, new ItemStack(Items.dye, 64, 15)); - chest.setInventorySlotContents(4, new ItemStack(Items.apple, 16)); - chest.setInventorySlotContents(5, new ItemStack(Blocks.sapling, 8, 0)); - chest.setInventorySlotContents(6, new ItemStack(Items.spawn_egg, 2, 90)); - chest.setInventorySlotContents(7, new ItemStack(Items.spawn_egg, 2, 91)); - chest.setInventorySlotContents(8, new ItemStack(Items.spawn_egg, 2, 92)); - chest.setInventorySlotContents(9, new ItemStack(Items.spawn_egg, 2, 93)); - if (chickenStick != null) { - chest.setInventorySlotContents(10, new ItemStack(chickenStick, 1)); + for (int[] pos : roots) { + world.setBlock(x + pos[0], y + pos[1], z + pos[2], Blocks.log, 12, 3); } } - - if (FTBIslands.getIslands() - .size() != 0) { - FTBIslands.getIslands() - .put( - islandName, - FTBIslands.islandLoc.get( - FTBIslands.getIslands() - .size() + 1)); - } else { - FTBIslands.getIslands() - .put(islandName, FTBIslands.islandLoc.get(1)); + } else { + for (int c = 0; c < 3; c++) { + for (int d = 0; d < 3; d++) { + world.setBlock(x + c, y, z + d, Blocks.dirt); + } } - FTBIslands.getIslands() - .put(islandName, new IslandPos(x, y, z)); - try { - FTBIslands.saveIslands(FTBIslands.getIslands()); - } catch (IOException e) { - e.printStackTrace(); + world.setBlock(x + 2, y + 1, z + 1, Blocks.chest); + world.getBlock(x + 2, y + 1, z + 1) + .rotateBlock(world, x + 2, y + 1, z + 1, ForgeDirection.WEST); + TileEntityChest chest = (TileEntityChest) world.getTileEntity(x + 2, y + 1, z + 1); + + chest.setInventorySlotContents(0, new ItemStack(Blocks.flowing_water, 1)); + chest.setInventorySlotContents(1, new ItemStack(Blocks.flowing_lava, 1)); + chest.setInventorySlotContents(2, new ItemStack(Items.dye, 64, 15)); + chest.setInventorySlotContents(3, new ItemStack(Items.dye, 64, 15)); + chest.setInventorySlotContents(4, new ItemStack(Items.apple, 16)); + chest.setInventorySlotContents(5, new ItemStack(Blocks.sapling, 8, 0)); + chest.setInventorySlotContents(6, new ItemStack(Items.spawn_egg, 2, 90)); + chest.setInventorySlotContents(7, new ItemStack(Items.spawn_egg, 2, 91)); + chest.setInventorySlotContents(8, new ItemStack(Items.spawn_egg, 2, 92)); + chest.setInventorySlotContents(9, new ItemStack(Items.spawn_egg, 2, 93)); + if (chickenStick != null) { + chest.setInventorySlotContents(10, new ItemStack(chickenStick, 1)); } - FTBIslands.logger.info(String.format("Created island named %s at %d %d %d", islandName, x, y, z)); - return true; - } else { - return false; - } - } - - protected static void save() { - try { - FTBIslands.saveIslands(FTBIslands.getIslands()); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public static class IslandPos implements Serializable { - - private int x; - private int y; - private int z; - - public IslandPos(int x, int y, int z) { - this.x = x; - this.y = y; - this.z = z; } - public int getX() { - return x; - } - - public int getY() { - return y; - } - - public int getZ() { - return z; - } + IslandContainer container = FTBIslands.getIslandStorage() + .getContainer(); + int dim = owner.worldObj.provider.dimensionId; + container.getIslands() + .add( + new Island( + islandName, + owner.getGameProfile() + .getId(), + new Island.Position(x, y, z, dim))); + container.incrementIslandsCreated(dim); + FTBIslands.getIslandStorage() + .saveContainer(); + FTBIslands.logger.info(String.format("Created island named %s at %d %d %d", islandName, x, y, z)); } } diff --git a/src/main/java/com/cricketcraft/ftbisland/IslandStorage.java b/src/main/java/com/cricketcraft/ftbisland/IslandStorage.java new file mode 100644 index 0000000..bcd78d5 --- /dev/null +++ b/src/main/java/com/cricketcraft/ftbisland/IslandStorage.java @@ -0,0 +1,55 @@ +package com.cricketcraft.ftbisland; + +import java.io.*; +import java.util.ArrayList; +import java.util.HashMap; + +import org.apache.commons.io.FileUtils; + +import com.cricketcraft.ftbisland.model.IslandContainer; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; + +public class IslandStorage { + + private final File islandFile = new File(FTBIslands.getDirectory(), "islands.json"); + private IslandContainer container; + + public IslandContainer getContainer() { + return container; + } + + public void saveContainer() { + try { + islandFile.createNewFile(); + try (FileWriter writer = new FileWriter(islandFile)) { + new GsonBuilder().setPrettyPrinting() + .create() + .toJson(container, writer); + } + } catch (IOException e) { + FTBIslands.logger.warn("Could not save islands to file"); + e.printStackTrace(); + } + } + + public void reloadContainer() { + try { + IslandContainer containerFromFile = this.getContainerFromFile(); + if (containerFromFile == null) containerFromFile = new IslandContainer(new ArrayList<>(), new HashMap<>()); + this.container = containerFromFile; + } catch (IOException e) { + FTBIslands.logger.error("Couldn't get islands from save file"); + e.printStackTrace(); + } + } + + private IslandContainer getContainerFromFile() throws IOException { + islandFile.createNewFile(); + try (FileInputStream ignored = new FileInputStream(islandFile)) { + return new Gson() + .fromJson(FileUtils.readFileToString(islandFile), new TypeToken() {}.getType()); + } + } +} diff --git a/src/main/java/com/cricketcraft/ftbisland/IslandUtils.java b/src/main/java/com/cricketcraft/ftbisland/IslandUtils.java index 7aa7108..baf21af 100644 --- a/src/main/java/com/cricketcraft/ftbisland/IslandUtils.java +++ b/src/main/java/com/cricketcraft/ftbisland/IslandUtils.java @@ -1,83 +1,111 @@ package com.cricketcraft.ftbisland; +import java.util.*; + import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.util.ChatComponentText; import net.minecraft.world.World; -public class IslandUtils { +import org.apache.commons.lang3.tuple.Pair; - public static boolean createIsland(World world, String islandName) { - FTBIslands.reloadIslands(); - if (FTBIslands.getIslands() == null) { - FTBIslands.logger.info("Island locations are null?? Empty possibly."); - return false; - } - IslandCreator.IslandPos pos = FTBIslands.islandLoc.get( - FTBIslands.getIslands() - .size() + 1); - IslandCreator.spawnIslandAt(world, pos.getX(), pos.getY(), pos.getZ(), islandName); - return true; - } +import com.cricketcraft.ftbisland.model.Island; + +public class IslandUtils { - public static void renameIsland(String oldName, String newName) { - FTBIslands.reloadIslands(); - IslandCreator.IslandPos pos = FTBIslands.getIslands() - .get(oldName); - FTBIslands.getIslands() - .remove(oldName); - FTBIslands.getIslands() - .put(newName, pos); - IslandCreator.save(); + public static void createIsland(World world, String islandName, EntityPlayer owner) { + FTBIslands.getIslandStorage() + .reloadContainer(); + Pair spiralLoc = calculateSpiral( + FTBIslands.getIslandStorage() + .getContainer() + .getIslandsCreated(owner.worldObj.provider.dimensionId)); + IslandCreator.spawnIslandAt( + world, + spiralLoc.getLeft() * 512 + 256, + 64, + spiralLoc.getRight() * 512 + 256, + islandName, + owner); } - public static void setSpawnForIsland(String s, int x, int y, int z) { - IslandCreator.IslandPos pos = new IslandCreator.IslandPos(x, y, z); - FTBIslands.getIslands() - .remove(s); - FTBIslands.getIslands() - .put(s, pos); - IslandCreator.save(); + public static void renameIsland(Island island, String newName) { + island.setName(newName); + FTBIslands.getIslandStorage() + .saveContainer(); } public static void joinIsland(String islandName, EntityPlayer player) { - if (player == null) { - FTBIslands.logger.info("The join command must be run in game."); + FTBIslands.getIslandStorage() + .reloadContainer(); + Optional island = FTBIslands.getIslandStorage() + .getContainer() + .getIslandByName(islandName); + if (island.isPresent()) { + Island.Position pos = island.get() + .getPos(); + if (player.dimension != pos.getDim()) { + player.travelToDimension(pos.getDim()); + } + int x = pos.getX(); + int y = pos.getY(); + int z = pos.getZ(); + int height = FTBIslands.islandType.equalsIgnoreCase("tree") ? 6 : 2; + double xAndZ = FTBIslands.islandType.equalsIgnoreCase("grass") ? 0.5 : 1.5; + if (player instanceof EntityPlayerMP) { + EntityPlayerMP playerMP = (EntityPlayerMP) player; + playerMP.setPositionAndUpdate(x + xAndZ, y + height, z + xAndZ); + // ChunkCoordinates chunk = new ChunkCoordinates(x, y, z); + // playerMP.setSpawnChunk(chunk, true); + } } else { - FTBIslands.reloadIslands(); - if (FTBIslands.getIslands() - .containsKey(islandName)) { - IslandCreator.IslandPos pos = new IslandCreator.IslandPos(0, 60, 0); - for (String key : FTBIslands.getIslands() - .keySet()) { - if (key.equalsIgnoreCase(islandName)) { - pos = FTBIslands.getIslands() - .get(key); - } - } - if (player.dimension != 0) { - player.travelToDimension(0); - } - int x = pos.getX(); - int y = pos.getY(); - int z = pos.getZ(); - int height = FTBIslands.islandType.equalsIgnoreCase("tree") ? 6 : 2; - double xAndZ = FTBIslands.islandType.equalsIgnoreCase("grass") ? 0.5 : 1.5; - if (player instanceof EntityPlayerMP) { - EntityPlayerMP playerMP = (EntityPlayerMP) player; - playerMP.setPositionAndUpdate(x + xAndZ, y + height, z + xAndZ); - // ChunkCoordinates chunk = new ChunkCoordinates(x, y, z); - // playerMP.setSpawnChunk(chunk, true); + player.addChatComponentMessage(new ChatComponentText("Island does not exist!")); + } + } + + public static void deleteIsland(Island island) { + FTBIslands.getIslandStorage() + .getContainer() + .getIslands() + .remove(island); + FTBIslands.getIslandStorage() + .saveContainer(); + } + + private static Pair calculateSpiral(int index) { + if (index == 0) { + return Pair.of(0, 0); + } + + // current position (x, z) and how much of current segment we passed + int x = 0; + int z = 0; + + int dx = 0; + int dz = 1; + int segmentLength = 1; + int segmentPassed = 0; + + for (int n = 0; n < index; n++) { + x += dx; + z += dz; + segmentPassed++; + + if (segmentPassed == segmentLength) { + segmentPassed = 0; + + // 'rotate' directions + int buffer = dz; + dz = -dx; + dx = buffer; + + // increase segment length if necessary + if (dx == 0) { + segmentLength++; } - } else { - player.addChatComponentMessage(new ChatComponentText("Island does not exist!")); } } - } - public static void deleteIsland(String islandName) { - FTBIslands.getIslands() - .remove(islandName); - IslandCreator.save(); + return Pair.of(x, z); } } diff --git a/src/main/java/com/cricketcraft/ftbisland/commands/CreateIslandCommand.java b/src/main/java/com/cricketcraft/ftbisland/commands/CreateIslandCommand.java index e242b1e..c69242b 100644 --- a/src/main/java/com/cricketcraft/ftbisland/commands/CreateIslandCommand.java +++ b/src/main/java/com/cricketcraft/ftbisland/commands/CreateIslandCommand.java @@ -24,13 +24,14 @@ public String getCommandUsage(ICommandSender ics) { @Override public IChatComponent onCommand(ICommandSender iCommandSender, String[] strings) throws CommandException { checkArgs(strings, 1); - FTBIslands.reloadIslands(); - if (FTBIslands.getIslands() - .containsKey(strings[0])) { + FTBIslands.getIslandStorage() + .reloadContainer(); + if (FTBIslands.getIslandStorage() + .getContainer() + .doesIslandExist(strings[0])) { return error(FTBIslands.mod.chatComponent("cmd.create_exists", strings[0])); } - boolean success = IslandUtils.createIsland(iCommandSender.getEntityWorld(), strings[0]); - return success ? FTBIslands.mod.chatComponent("cmd.create_success", strings[0]) - : error(FTBIslands.mod.chatComponent("cmd.create_fail", strings[0])); + IslandUtils.createIsland(iCommandSender.getEntityWorld(), strings[0], getCommandSenderAsPlayer(iCommandSender)); + return FTBIslands.mod.chatComponent("cmd.create_success", strings[0]); } } diff --git a/src/main/java/com/cricketcraft/ftbisland/commands/DeleteIslandCommand.java b/src/main/java/com/cricketcraft/ftbisland/commands/DeleteIslandCommand.java index 6be38ec..1c79f97 100644 --- a/src/main/java/com/cricketcraft/ftbisland/commands/DeleteIslandCommand.java +++ b/src/main/java/com/cricketcraft/ftbisland/commands/DeleteIslandCommand.java @@ -1,11 +1,14 @@ package com.cricketcraft.ftbisland.commands; +import java.util.Optional; + import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; import net.minecraft.util.IChatComponent; import com.cricketcraft.ftbisland.FTBIslands; import com.cricketcraft.ftbisland.IslandUtils; +import com.cricketcraft.ftbisland.model.Island; import ftb.lib.api.cmd.CommandLM; import ftb.lib.api.cmd.CommandLevel; @@ -23,20 +26,23 @@ public String getCommandUsage(ICommandSender ics) { @Override public String[] getTabStrings(ICommandSender ics, String[] args, int i) throws CommandException { - return i == 0 ? FTBIslands.getIslands() - .keySet() - .toArray(new String[0]) : null; + return i == 0 ? FTBIslands.getIslandStorage() + .getContainer() + .getAllIslandNames() : null; } @Override public IChatComponent onCommand(ICommandSender iCommandSender, String[] strings) throws CommandException { checkArgs(strings, 1); - FTBIslands.reloadIslands(); - if (!FTBIslands.getIslands() - .containsKey(strings[0])) { + FTBIslands.getIslandStorage() + .reloadContainer(); + Optional island = FTBIslands.getIslandStorage() + .getContainer() + .getIslandByName(strings[0]); + if (!island.isPresent()) { return error(FTBIslands.mod.chatComponent("cmd.delete_not_exist", strings[0])); } - IslandUtils.deleteIsland(strings[0]); + IslandUtils.deleteIsland(island.get()); return FTBIslands.mod.chatComponent("cmd.delete_success", strings[0]); } } diff --git a/src/main/java/com/cricketcraft/ftbisland/commands/JoinIslandCommand.java b/src/main/java/com/cricketcraft/ftbisland/commands/JoinIslandCommand.java index ba1f710..7c9c6ac 100644 --- a/src/main/java/com/cricketcraft/ftbisland/commands/JoinIslandCommand.java +++ b/src/main/java/com/cricketcraft/ftbisland/commands/JoinIslandCommand.java @@ -23,9 +23,9 @@ public String getCommandUsage(ICommandSender ics) { @Override public String[] getTabStrings(ICommandSender ics, String[] args, int i) throws CommandException { - return i == 0 ? FTBIslands.getIslands() - .keySet() - .toArray(new String[0]) : null; + return i == 0 ? FTBIslands.getIslandStorage() + .getContainer() + .getAllIslandNames() : null; } @Override @@ -34,39 +34,4 @@ public IChatComponent onCommand(ICommandSender iCommandSender, String[] strings) IslandUtils.joinIsland(strings[0], getCommandSenderAsPlayer(iCommandSender)); return FTBIslands.mod.chatComponent("cmd.join_success", strings[0]); } - // - // @Override - // public String getCommandName() { - // return "island_join"; - // } - // - // @Override - // public String getCommandUsage(ICommandSender sender) { - // return "island_join "; - // } - // - // @Override - // public List addTabCompletionOptions(ICommandSender sender, String[] input) { - // return input.length == 1 ? getListOfStringsMatchingLastWord(input, getPlayers()) : null; - // } - // - // protected String[] getPlayers() { - // return MinecraftServer.getServer() - // .getAllUsernames(); - // } - // - // @Override - // public void processCommand(ICommandSender sender, String[] input) { - // if (input.length != 1) { - // sender.addChatMessage(new ChatComponentText("Invalid arguments!")); - // return; - // } - // - // IslandUtils.joinIsland(input[0], getCommandSenderAsPlayer(sender)); - // } - // - // @Override - // public boolean canCommandSenderUseCommand(ICommandSender sender) { - // return true; - // } } diff --git a/src/main/java/com/cricketcraft/ftbisland/commands/ListIslandCommand.java b/src/main/java/com/cricketcraft/ftbisland/commands/ListIslandCommand.java index d4535b0..57fdf87 100644 --- a/src/main/java/com/cricketcraft/ftbisland/commands/ListIslandCommand.java +++ b/src/main/java/com/cricketcraft/ftbisland/commands/ListIslandCommand.java @@ -23,10 +23,13 @@ public String getCommandUsage(ICommandSender ics) { @Override public IChatComponent onCommand(ICommandSender iCommandSender, String[] strings) throws CommandException { - FTBIslands.reloadIslands(); - FTBIslands.getIslands() - .keySet() - .forEach(key -> iCommandSender.addChatMessage(new ChatComponentText(key))); + FTBIslands.getIslandStorage() + .reloadContainer(); + for (String name : FTBIslands.getIslandStorage() + .getContainer() + .getAllIslandNames()) { + iCommandSender.addChatMessage(new ChatComponentText(name)); + } return null; } } diff --git a/src/main/java/com/cricketcraft/ftbisland/commands/RenameIslandCommand.java b/src/main/java/com/cricketcraft/ftbisland/commands/RenameIslandCommand.java index 22108ca..66a346b 100644 --- a/src/main/java/com/cricketcraft/ftbisland/commands/RenameIslandCommand.java +++ b/src/main/java/com/cricketcraft/ftbisland/commands/RenameIslandCommand.java @@ -1,11 +1,14 @@ package com.cricketcraft.ftbisland.commands; +import java.util.Optional; + import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; import net.minecraft.util.IChatComponent; import com.cricketcraft.ftbisland.FTBIslands; import com.cricketcraft.ftbisland.IslandUtils; +import com.cricketcraft.ftbisland.model.Island; import ftb.lib.api.cmd.CommandLM; import ftb.lib.api.cmd.CommandLevel; @@ -23,24 +26,28 @@ public String getCommandUsage(ICommandSender ics) { @Override public String[] getTabStrings(ICommandSender ics, String[] args, int i) throws CommandException { - return i == 0 ? FTBIslands.getIslands() - .keySet() - .toArray(new String[0]) : null; + return i == 0 ? FTBIslands.getIslandStorage() + .getContainer() + .getAllIslandNames() : null; } @Override public IChatComponent onCommand(ICommandSender iCommandSender, String[] strings) throws CommandException { checkArgs(strings, 2); - FTBIslands.reloadIslands(); - if (!FTBIslands.getIslands() - .containsKey(strings[0])) { + FTBIslands.getIslandStorage() + .reloadContainer(); + Optional island = FTBIslands.getIslandStorage() + .getContainer() + .getIslandByName(strings[0]); + if (!island.isPresent()) { return error(FTBIslands.mod.chatComponent("cmd.rename_old_not_exist", strings[0])); } - if (FTBIslands.getIslands() - .containsKey(strings[1])) { + if (FTBIslands.getIslandStorage() + .getContainer() + .doesIslandExist(strings[1])) { return error(FTBIslands.mod.chatComponent("cmd.rename_new_exists", strings[1])); } - IslandUtils.renameIsland(strings[0], strings[1]); + IslandUtils.renameIsland(island.get(), strings[1]); return FTBIslands.mod.chatComponent("cmd.rename_success", strings[0], strings[1]); } } diff --git a/src/main/java/com/cricketcraft/ftbisland/commands/SetIslandSpawnCommand.java b/src/main/java/com/cricketcraft/ftbisland/commands/SetIslandSpawnCommand.java deleted file mode 100644 index 2dee4e2..0000000 --- a/src/main/java/com/cricketcraft/ftbisland/commands/SetIslandSpawnCommand.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.cricketcraft.ftbisland.commands; - -import net.minecraft.command.CommandException; -import net.minecraft.command.ICommandSender; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.util.IChatComponent; - -import com.cricketcraft.ftbisland.FTBIslands; -import com.cricketcraft.ftbisland.IslandUtils; - -import ftb.lib.api.cmd.CommandLM; -import ftb.lib.api.cmd.CommandLevel; - -public class SetIslandSpawnCommand extends CommandLM { - - public SetIslandSpawnCommand() { - super("island_setspawn", CommandLevel.OP); - } - - @Override - public String getCommandUsage(ICommandSender ics) { - return "/" + this.getCommandName() + " [ ]"; - } - - @Override - public String[] getTabStrings(ICommandSender ics, String[] args, int i) throws CommandException { - return i == 0 ? FTBIslands.getIslands() - .keySet() - .toArray(new String[0]) : null; - } - - @Override - public IChatComponent onCommand(ICommandSender iCommandSender, String[] strings) throws CommandException { - // Allow either just islandName or expect full coordinate set - if (strings.length != 1) { - checkArgs(strings, 4); - } - FTBIslands.reloadIslands(); - if (!FTBIslands.getIslands() - .containsKey(strings[0])) { - return error(FTBIslands.mod.chatComponent("cmd.setspawn_not_exist", strings[0])); - } - int x, y, z; - if (strings.length == 4) { - x = Integer.parseInt(strings[1]); - y = Integer.parseInt(strings[2]); - z = Integer.parseInt(strings[3]); - } else { - EntityPlayerMP player = getCommandSenderAsPlayer(iCommandSender); - x = (int) player.posX; - y = (int) player.posY; - z = (int) player.posZ; - } - IslandUtils.setSpawnForIsland(strings[0], x, y, z); - return FTBIslands.mod.chatComponent("cmd.setspawn_success", strings[0], x, y, z); - } -} diff --git a/src/main/java/com/cricketcraft/ftbisland/commands/TeleportIslandCommand.java b/src/main/java/com/cricketcraft/ftbisland/commands/TeleportIslandCommand.java index 32ebe28..f26130f 100644 --- a/src/main/java/com/cricketcraft/ftbisland/commands/TeleportIslandCommand.java +++ b/src/main/java/com/cricketcraft/ftbisland/commands/TeleportIslandCommand.java @@ -28,9 +28,9 @@ public Boolean getUsername(String[] args, int i) { @Override public String[] getTabStrings(ICommandSender ics, String[] args, int i) throws CommandException { - return i == 0 ? FTBIslands.getIslands() - .keySet() - .toArray(new String[0]) : super.getTabStrings(ics, args, i); + return i == 0 ? FTBIslands.getIslandStorage() + .getContainer() + .getAllIslandNames() : super.getTabStrings(ics, args, i); } @Override diff --git a/src/main/java/com/cricketcraft/ftbisland/model/Island.java b/src/main/java/com/cricketcraft/ftbisland/model/Island.java new file mode 100644 index 0000000..694b557 --- /dev/null +++ b/src/main/java/com/cricketcraft/ftbisland/model/Island.java @@ -0,0 +1,64 @@ +package com.cricketcraft.ftbisland.model; + +import java.util.UUID; + +public class Island { + + private String name; + private final UUID owner; + + private final Position pos; + + public Island(String name, UUID owner, Position pos) { + this.name = name; + this.owner = owner; + this.pos = pos; + } + + public UUID getOwner() { + return owner; + } + + public Position getPos() { + return pos; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public static class Position { + + private final int x; + private final int y; + private final int z; + private final int dim; + + public Position(int x, int y, int z, int dim) { + this.x = x; + this.y = y; + this.z = z; + this.dim = dim; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + public int getZ() { + return z; + } + + public int getDim() { + return dim; + } + } +} diff --git a/src/main/java/com/cricketcraft/ftbisland/model/IslandContainer.java b/src/main/java/com/cricketcraft/ftbisland/model/IslandContainer.java new file mode 100644 index 0000000..59de39e --- /dev/null +++ b/src/main/java/com/cricketcraft/ftbisland/model/IslandContainer.java @@ -0,0 +1,58 @@ +package com.cricketcraft.ftbisland.model; + +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; + +public class IslandContainer { + + private final List islands; + + private final Map islandsCreated; + + public IslandContainer(List islands, Map islandsCreated) { + this.islands = islands; + this.islandsCreated = islandsCreated; + } + + public List getIslands() { + return islands; + } + + public int getIslandsCreated(int dim) { + Integer islandsCreatedInDim = islandsCreated.get(dim); + return islandsCreatedInDim != null ? islandsCreatedInDim : 0; + } + + public void incrementIslandsCreated(int dim) { + islandsCreated.merge(dim, 1, Integer::sum); + } + + public Optional getIslandByName(String name) { + return this.islands.stream() + .filter( + island -> island.getName() + .equals(name)) + .findFirst(); + } + + public boolean doesIslandExist(String name) { + return this.getIslandByName(name) + .isPresent(); + } + + public long islandsOwnedByUser(UUID owner) { + return this.islands.stream() + .filter( + island -> island.getOwner() + .equals(owner)) + .count(); + } + + public String[] getAllIslandNames() { + return this.islands.stream() + .map(Island::getName) + .toArray(String[]::new); + } +}