diff --git a/DummyCore/Core/CoreInitialiser.java b/DummyCore/Core/CoreInitialiser.java index 1c2bda1..7d5c426 100644 --- a/DummyCore/Core/CoreInitialiser.java +++ b/DummyCore/Core/CoreInitialiser.java @@ -1,5 +1,6 @@ package DummyCore.Core; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -26,6 +27,8 @@ import DummyCore.Items.ItemRegistry; import DummyCore.Items.MultiItem; import DummyCore.Utils.ColoredLightHandler; import DummyCore.Utils.DummyConfig; +import DummyCore.Utils.DummyDataUtils; +import DummyCore.Utils.DummyPacketHandler; import DummyCore.Utils.EnumLightColor; import DummyCore.Utils.MathUtils; import DummyCore.Utils.Notifier; @@ -40,7 +43,10 @@ import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; import cpw.mods.fml.common.event.FMLInterModComms.IMCEvent; +import cpw.mods.fml.common.event.FMLServerStartingEvent; +import cpw.mods.fml.common.event.FMLServerStoppingEvent; import cpw.mods.fml.common.network.NetworkMod; +import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.registry.EntityRegistry; import cpw.mods.fml.common.registry.GameRegistry; import cpw.mods.fml.common.registry.TickRegistry; @@ -52,7 +58,7 @@ import cpw.mods.fml.relauncher.Side; * @version From DummyCore 1.0 */ @Mod(modid = "DummyCore", name = "DummyCore", version = "1.1dev", useMetadata = true) -@NetworkMod(clientSideRequired = true, serverSideRequired = false, channels = "DummyCore") +@NetworkMod(clientSideRequired = true, serverSideRequired = false, channels = {"DC.Packet.S","DC.Packet.C"}) public class CoreInitialiser extends DummyModContainer{ public static final CoreInitialiser instance = new CoreInitialiser(); private static DummyConfig cfg = new DummyConfig(); @@ -97,6 +103,9 @@ public class CoreInitialiser extends DummyModContainer{ { RenderingRegistry.registerEntityRenderingHandler(ColoredLightHandler.class, new RendererColoredLight()); } + DummyPacketHandler handler = new DummyPacketHandler(); + NetworkRegistry.instance().registerChannel(handler, "DC.Packet.S"); + NetworkRegistry.instance().registerChannel(handler, "DC.Packet.C"); } @EventHandler @@ -105,6 +114,18 @@ public class CoreInitialiser extends DummyModContainer{ Notifier.notifySimple(MultiItem.items+" MultiItems registered."); Notifier.notifySimple(MultiBlock.blocks+" MultiBlocks registered."); } + + @EventHandler + public void onServerStart(FMLServerStartingEvent e) + { + DummyDataUtils.load(e); + } + + @EventHandler + public void onServerStop(FMLServerStoppingEvent e) + { + DummyDataUtils.stop(); + } public static MultiItem mItem; public static MultiBlock mBlock; diff --git a/DummyCore/Utils/DummyDataUtils.java b/DummyCore/Utils/DummyDataUtils.java new file mode 100644 index 0000000..b9e4c74 --- /dev/null +++ b/DummyCore/Utils/DummyDataUtils.java @@ -0,0 +1,219 @@ +package DummyCore.Utils; + +import java.io.File; +import java.io.IOException; +import java.util.Hashtable; + +import javax.naming.spi.DirectoryManager; + +import net.minecraftforge.common.Configuration; + +import org.apache.commons.io.DirectoryWalker; +import org.apache.commons.io.filefilter.DirectoryFileFilter; +import org.bouncycastle.asn1.x500.DirectoryString; + +import cpw.mods.fml.common.discovery.DirectoryDiscoverer; +import cpw.mods.fml.common.event.FMLServerStartingEvent; + +/** + * + * @author Modbder + * @version From DummyCore 1.4 + * @Description used to store any String for players/worlds in the world save folder and get it. + */ +public class DummyDataUtils { + private static File mainWorldFile; + private static Configuration globalConfig; + private static Hashtable playerFiles = new Hashtable(); + private static Hashtable playerConfigs = new Hashtable(); + private static String getPath; + private static boolean isWorking; + + public static void load(FMLServerStartingEvent event) + { + String name = event.getServer().getFile(event.getServer().getFolderName()).getAbsolutePath(); + System.out.println(name); + int length = 0; + for(int i = 0; i < name.length(); ++i) + { + if(name.substring(0, i).contains("\\.\\")) + { + --length; + --length; + break; + } + ++length; + } + String print = name.substring(0,length); + print += "saves\\"; + print += event.getServer().getFolderName(); + print += "\\DummyData\\"; + File f = new File(print); + f.mkdirs(); + File file = new File(print+"GlobalData.ddat"); + try { + if(!file.exists()) + file.createNewFile(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return; + } + File f1 = new File(print+"/PlayerData/"); + f1.mkdirs(); + getPath = print; + mainWorldFile = file; + globalConfig = new Configuration(file); + isWorking = true; + } + + public static void stop() + { + mainWorldFile = null; + globalConfig = null; + playerFiles.clear(); + playerConfigs.clear(); + getPath = "no path"; + isWorking = false; + } + + private static File getDataFileForPlayer(String playerName) + { + if(!playerFiles.containsKey(playerName)) + { + File ret = new File(getPath+"/PlayerData/"+playerName+".ddat"); + if(!ret.exists()) + try { + ret.createNewFile(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return null; + } + playerFiles.put(playerName, ret); + return ret; + }else + { + return playerFiles.get(playerName); + } + + } + + private static Configuration getDataConfigForPlayer(String playerName) + { + if(!canWorkWithData())return null; + if(!playerConfigs.containsKey(playerName)) + { + playerConfigs.put(playerName, new Configuration(getDataFileForPlayer(playerName))); + } + return playerConfigs.get(playerName); + } + + /** + * Writes the given String to the GlobalData.ddat file with the key of dataName + * @version From DummyCore 1.4 + * @param modid - the id of your mod + * @param dataName - key of the data + * @param dataString - data to store + */ + public static void writeCustomDataForMod(String modid, String dataName, String dataString) + { + if(!canWorkWithData())return; + globalConfig.load(); + globalConfig.get(modid, dataName, dataString).set(dataString); + globalConfig.save(); + } + + + /** + * Writes the given String to the GlobalData.ddat file with the key of modid + * @version From DummyCore 1.4 + * @param modid - the id of your mod + * @param dataString - data to store + */ + public static void writeGlobalDataForMod(String modid, String dataString) + { + if(!canWorkWithData())return; + globalConfig.load(); + globalConfig.get(modid, modid, dataString).set(dataString); + globalConfig.save(); + } + + + /** + * Returns the String from the GlobalData.ddat file with the key of modid + * @version From DummyCore 1.4 + * @param modid - the id of your mod + * @return a string with the data, written to the ddat file. Returns "no data" if none was found + */ + public static String getGlobalDataForMod(String modid) + { + if(!canWorkWithData())return null; + globalConfig.load(); + String ret = globalConfig.get(modid, modid, "no data").getString(); + globalConfig.save(); + return ret; + } + + /** + * Returns the String from the GlobalData.ddat file with the custom key + * @version From DummyCore 1.4 + * @param modid - the id of your mod + * @param dataName - key of the data + * @return a string with the data, written to the ddat file. Returns "no data" if none was found + */ + public static String getCustomDataForMod(String modid, String dataName) + { + if(!canWorkWithData())return null; + globalConfig.load(); + String ret = globalConfig.get(modid, dataName, "no data").getString(); + globalConfig.save(); + return ret; + } + + /** + * Sets the given data to the ddat file of the player with given name. + * @version From DummyCore 1.4 + * @param playerName - name of the player + * @param modid - id of the mod + * @param dataName - key of the data + * @param dataValue - the data to store + */ + public static void setDataForPlayer(String playerName, String modid, String dataName, String dataValue) + { + if(!canWorkWithData())return; + Configuration config = getDataConfigForPlayer(playerName); + config.load(); + config.get(modid, dataName, dataValue).set(dataValue); + config.save(); + } + + /** + * Returns the String from the ddat file of the given player with the custom key + * @version From DummyCore 1.4 + * @param playerName - name of the player + * @param modid - id of the mod + * @param dataName - key of the data + * @return a string with the data, written to the ddat file. Returns "no data" if none was found + */ + public static String getDataForPlayer(String playerName, String modid, String dataName) + { + if(!canWorkWithData())return null; + Configuration config = getDataConfigForPlayer(playerName); + config.load(); + String ret = config.get(modid, dataName, "no data").getString(); + config.save(); + return ret; + } + + /** + * Used to check, if current world exists(for example, if the player is in the main menu returns false) + * @version From DummyCore 1.4 + * @return True, if there is a running world, false if not. + */ + public static boolean canWorkWithData() + { + return isWorking; + } + +} diff --git a/DummyCore/Utils/DummyMenuAPI.java b/DummyCore/Utils/DummyMenuAPI.java new file mode 100644 index 0000000..f8ffb2e --- /dev/null +++ b/DummyCore/Utils/DummyMenuAPI.java @@ -0,0 +1,82 @@ +package DummyCore.Utils; + +import java.util.ArrayList; +import java.util.List; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.GuiButtonLanguage; +import net.minecraft.client.gui.ScaledResolution; +import net.minecraft.client.resources.I18n; +import net.minecraft.util.ResourceLocation; +import net.minecraft.world.storage.ISaveFormat; +import net.minecraft.world.storage.WorldInfo; + +public class DummyMenuAPI { + int height, width, ticks, scheduledTick, imageIndex; + Minecraft mc; + public static final List STANDART_MC_BUTTONS = new ArrayList(); + public static final ResourceLocation STANDART_MC_LETTERS = new ResourceLocation("textures/gui/title/minecraft.png"); + + public DummyMenuAPI(int height, int width) + { + this.width = width; + this.height = height; + mc = Minecraft.getMinecraft(); + initialiseButtonList(); + } + + public void renderStandartMenu(IMainMenu menu) + { + ResourceLocation[] loc = menu.getLetters(); + + } + + private void initialiseButtonList() + { + int i = height / 4 + 48; + STANDART_MC_BUTTONS.clear(); + STANDART_MC_BUTTONS.add(new GuiButton(0, width / 2 - 100, i + 72 + 12, 98, 20, I18n.getString("menu.options"))); + if (this.mc.isDemo()) + { + this.addDemoButtons(i, 24); + } + else + { + this.addSingleplayerMultiplayerButtons(i, 24); + } + STANDART_MC_BUTTONS.add(new GuiButton(0, this.width / 2 - 100, i + 72 + 12, 98, 20, I18n.getString("menu.options"))); + STANDART_MC_BUTTONS.add(new GuiButton(4, this.width / 2 + 2, i + 72 + 12, 98, 20, I18n.getString("menu.quit"))); + STANDART_MC_BUTTONS.add(new GuiButtonLanguage(5, this.width / 2 - 124, i + 72 + 12)); + } + + private void addDemoButtons(int par1, int par2) + { + GuiButton buttonResetDemo; + STANDART_MC_BUTTONS.add(new GuiButton(11, this.width / 2 - 100, par1, I18n.getString("menu.playdemo"))); + STANDART_MC_BUTTONS.add(buttonResetDemo = new GuiButton(12, this.width / 2 - 100, par1 + par2 * 1, I18n.getString("menu.resetdemo"))); + ISaveFormat isaveformat = this.mc.getSaveLoader(); + WorldInfo worldinfo = isaveformat.getWorldInfo("Demo_World"); + + if (worldinfo == null) + { + buttonResetDemo.enabled = false; + } + } + + private void addSingleplayerMultiplayerButtons(int par1, int par2) + { + GuiButton fmlModButton; + GuiButton minecraftRealmsButton; + STANDART_MC_BUTTONS.add(new GuiButton(1, this.width / 2 - 100, par1, I18n.getString("menu.singleplayer"))); + STANDART_MC_BUTTONS.add(new GuiButton(2, this.width / 2 - 100, par1 + par2 * 1, I18n.getString("menu.multiplayer"))); + //If Minecraft Realms is enabled, halve the size of both buttons and set them next to eachother. + fmlModButton = new GuiButton(6, this.width / 2 - 100, par1 + par2 * 2, "Mods"); + STANDART_MC_BUTTONS.add(fmlModButton); + minecraftRealmsButton = new GuiButton(14, this.width / 2 - 100, par1 + par2 * 2, I18n.getString("menu.online")); + minecraftRealmsButton.xPosition = this.width / 2 - 100; + STANDART_MC_BUTTONS.add(minecraftRealmsButton); + minecraftRealmsButton.drawButton = false; + } + +} diff --git a/DummyCore/Utils/DummyPacketHandler.java b/DummyCore/Utils/DummyPacketHandler.java new file mode 100644 index 0000000..ef3bfc5 --- /dev/null +++ b/DummyCore/Utils/DummyPacketHandler.java @@ -0,0 +1,66 @@ +package DummyCore.Utils; + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; + +import net.minecraft.network.INetworkManager; +import net.minecraft.network.packet.Packet250CustomPayload; +import net.minecraft.tileentity.TileEntity; +import cpw.mods.fml.client.FMLClientHandler; +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.network.IPacketHandler; +import cpw.mods.fml.common.network.Player; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.server.FMLServerHandler; + +public class DummyPacketHandler implements IPacketHandler{ + + @Override + public void onPacketData(INetworkManager manager, + Packet250CustomPayload packet, Player player) { + int x = 0; + int y = 0; + int z = 0; + String gameData = "data"; + try { + Side s = FMLCommonHandler.instance().getEffectiveSide(); + if(s == Side.CLIENT) + { + ByteArrayInputStream b = new ByteArrayInputStream(packet.data, 0, packet.length); + DataInputStream dis = new DataInputStream(b); + if(packet.channel.contains("DC.Packet.C")) + { + x = dis.readInt(); + y = dis.readInt(); + z = dis.readInt(); + gameData = dis.readUTF(); + TileEntity t = FMLClientHandler.instance().getClient().theWorld.getBlockTileEntity(x, y, z); + if(t instanceof ITEHasGameData && gameData != "data") + { + ((ITEHasGameData)t).setData(DataStorage.parseData(gameData)); + } + } + }else + { + ByteArrayInputStream b = new ByteArrayInputStream(packet.data, 0, packet.length); + DataInputStream dis = new DataInputStream(b); + if(packet.channel.contains("DC.Packet.S")) + { + x = dis.readInt(); + y = dis.readInt(); + z = dis.readInt(); + gameData = dis.readUTF(); + TileEntity t = FMLServerHandler.instance().getServer().getEntityWorld().getBlockTileEntity(x, y, z); + if(t instanceof ITEHasGameData && gameData != "data") + { + ((ITEHasGameData)t).setData(DataStorage.parseData(gameData)); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + return; + } + } + +} diff --git a/DummyCore/Utils/EnumGuiPosition.java b/DummyCore/Utils/EnumGuiPosition.java new file mode 100644 index 0000000..6e1e8d2 --- /dev/null +++ b/DummyCore/Utils/EnumGuiPosition.java @@ -0,0 +1,18 @@ +package DummyCore.Utils; + +public enum EnumGuiPosition { + TOPLEFT, + TOPRIGHT, + BOTLEFT, + BOTRIGHT, + CENTER; + + public int x; + public int y; + EnumGuiPosition() + { + } + + + +} diff --git a/DummyCore/Utils/IMainMenu.java b/DummyCore/Utils/IMainMenu.java new file mode 100644 index 0000000..0ff65bf --- /dev/null +++ b/DummyCore/Utils/IMainMenu.java @@ -0,0 +1,27 @@ +package DummyCore.Utils; + +import java.util.List; + +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.gui.GuiButton; +import net.minecraft.util.ResourceLocation; + +public interface IMainMenu { + + public abstract ResourceLocation[] getBackgroundImages(); + + public abstract boolean shouldRenderFullScreen(int imageIndex); + + public abstract List getButtonList(); + + public abstract void render(); + + public abstract List getTextFor(EnumGuiPosition e); + + public abstract ResourceLocation[] getLetters(); + + public abstract boolean renderUnstandartLetters(); + + public abstract FontRenderer textRenderer(); + +} diff --git a/DummyCore/Utils/ITEHasGameData.java b/DummyCore/Utils/ITEHasGameData.java new file mode 100644 index 0000000..c8b593b --- /dev/null +++ b/DummyCore/Utils/ITEHasGameData.java @@ -0,0 +1,11 @@ +package DummyCore.Utils; + +public interface ITEHasGameData { + + public abstract String getData(); + + public abstract void setData(DummyData[] data); + + public abstract Coord3D getPosition(); + +} diff --git a/DummyCore/Utils/MathUtils.java b/DummyCore/Utils/MathUtils.java index 274c605..1f55fce 100644 --- a/DummyCore/Utils/MathUtils.java +++ b/DummyCore/Utils/MathUtils.java @@ -123,4 +123,50 @@ public class MathUtils { { return Math.sqrt(a*a); } + + public static boolean arrayContains(Object[] array, Object searched) + { + for(int i = 0; i < array.length; ++i) + { + if(array[i].equals(searched)) + return true; + } + return false; + } + + public static boolean arrayContains(int[] array, int searched) + { + for(int i = 0; i < array.length; ++i) + { + if(array[i] == searched) + return true; + } + return false; + } + + public static int getIntInArray(int[] array, int searched) + { + for(int i = 0; i < array.length; ++i) + { + if(array[i] == searched) + return i; + } + return -1; + } + + public static boolean isArrayTheSame(boolean[] array) + { + boolean previous = array[0]; + for(int i = 0; i < array.length; ++i) + { + if(array[i] == previous) + { + previous = array[i]; + }else + { + return false; + } + } + return true; + } } diff --git a/DummyCore/Utils/MiscUtils.java b/DummyCore/Utils/MiscUtils.java index 0694305..1ee1b26 100644 --- a/DummyCore/Utils/MiscUtils.java +++ b/DummyCore/Utils/MiscUtils.java @@ -1,5 +1,8 @@ package DummyCore.Utils; +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; + import net.minecraft.block.Block; import net.minecraft.client.Minecraft; import net.minecraft.entity.EntityLivingBase; @@ -7,11 +10,14 @@ import net.minecraft.entity.item.EntityItem; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.network.packet.Packet250CustomPayload; import net.minecraft.util.ResourceLocation; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraft.world.chunk.Chunk; import net.minecraft.world.chunk.storage.ExtendedBlockStorage; +import net.minecraftforge.oredict.OreDictionary; +import cpw.mods.fml.common.network.PacketDispatcher; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @@ -146,4 +152,95 @@ public class MiscUtils { return ret; } + + /** + * Adds the given ItemStack to the given inventory, also updating the inventory. + * @version From DummyCore 1.4 + * @param s - the stack to add + * @param i - the inventory for the stack + * @param remote - use world.isRemote here. This function is usually called on both sides, however, you can call it on server only, it will be ok, then put false in this field + * @return true if adding was successful, false if not. + */ + public static boolean addItemToInventory(ItemStack s, IInventory i, boolean remote) + { + + for(int p = 0; p < i.getSizeInventory();++p) + { + if(i.getStackInSlot(p) != null && i.getStackInSlot(p).itemID == s.itemID && i.getStackInSlot(p).getItemDamage() == s.getItemDamage() && i.getStackInSlot(p).stackSize+s.stackSize<=64) + { + ItemStack slot = i.getStackInSlot(p); + System.out.println(slot); + if(!remote) + { + i.setInventorySlotContents(p, new ItemStack(slot.itemID,slot.stackSize+s.stackSize,slot.getItemDamage())); + } + i.onInventoryChanged(); + return true; + } + } + for(int p = 0; p < i.getSizeInventory();++p) + { + if(i.getStackInSlot(p) == null && !remote) + { + i.setInventorySlotContents(p, s); + i.onInventoryChanged(); + return true; + } + } + return false; + } + + /** + * Used to check, if the Forge Ore Dictionary contains the given name in it. + * @version From DummyCore 1.4 + * @param oreName - the ore name to search + * @return true if OreDictionary cantains the given ore, false if not. + */ + public static boolean oreDictionaryContains(String oreName) + { + String[] names = OreDictionary.getOreNames(); + boolean ret = false; + for(int i = 0; i < names.length; ++i) + { + if(names[i].contains(oreName) || names[i] == oreName) + { + ret = true; + } + } + return ret; + } + + /** + * Used to sync the given tile entity with the given side using DummyCore packet handler. + * @version From DummyCore 1.4 + * @param t - the tileentity to sync. + * @param s - the side, that will accept the packet. + */ + public static void syncTileEntity(ITEHasGameData t, Side s) + { + String packetname = "DC.Packet."; + if(s == Side.CLIENT)packetname += "C"; + if(s == Side.SERVER)packetname += "S"; + Packet250CustomPayload m = new Packet250CustomPayload(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(255); + DataOutputStream outputStream = new DataOutputStream(bos); + try + { + outputStream.writeInt((int) t.getPosition().x); + outputStream.writeInt((int) t.getPosition().y); + outputStream.writeInt((int) t.getPosition().z); + outputStream.writeUTF(t.getData()); + }catch (Exception ex) + { + ex.printStackTrace(); + return; + } + m.channel = packetname; + m.data = bos.toByteArray(); + m.length = bos.size(); + if(s == Side.SERVER) + PacketDispatcher.sendPacketToServer(m);; + if(s == Side.CLIENT) + PacketDispatcher.sendPacketToAllPlayers(m); + } }