Jump to content

HappyKiller1O1

Members
  • Posts

    581
  • Joined

  • Last visited

Everything posted by HappyKiller1O1

  1. It's pretty simple to get a player when you and they are on a server together. I presume it is FAR more difficult to do so when you just want to get that information without being on the server with them. I am not sure how to go about this, but I would assume it would require accessing Minecraft account servers that hold all that information.
  2. The only reason why I need to, is because I want the server owner to be able to change weight values, if wanted, for any item. So, I have it set up where I made a public, static Map in my ModReference class. I use that map to write the values of the json to, and read for the client to display. With that, all I would need to do is create the packet, register it on Server side only, and use that static Map (which should be on both sides I believe) to get the new values over, correct?
  3. Sorry about that. Anyway, I've remade it to only read everything once. My question is, is the config path different for a server? Or can I just check if the world is not remote, and load everything from the same path?
  4. Sorry about that Ernio, I made this thread last night at about 2 A.M. just to push me in the right direction. Anyway, I never thought about creating a map to be read from for the values. This is considering every time I look up the values, I search through the json. I will try what you're saying, but I would like to ask: for client side, would it be more efficient to write the json, then add those values to a map that is read from? Rather than parsing the json every time I need to look an item up? I'm guessing it would be, I'd just like your opinion. And, sorry about my question to Choonster. Didn't seem to understand his answer too well. And finally, thank you for the offer with Skyping you. I think I have it from here, nothing too big that I'd need more help with as of now.
  5. So, I would be creating an IThreadListener in the event? And, do I have to create the json on the server, or by creating in the config, will it already do it?
  6. I should explain a bit more: I need to know how exactly I would gather that information to send. What event would I use? Can I simply check if the world is not remote and that will get the server json? Just so you know, as I should of mentioned in my post; I understand networking, and use Simple Network Implementation.
  7. So, after creating my working json in the config directory, I would like to send an update to the client (if joining a server) with the server's information from the json, rather than the clients (so basically, tell the client that the server json must be used for this information). I am not quite sure how to go about this, and if anyone could point be in the correct direction, I would be very grateful.
  8. Yes, it is always true as long as you have something in your inventory. Now, this was a problem, considering when the final item was removed, it wouldn't update. All I had to do was move how I set my weight into the checker for the inventoryChanged boolean, and only run the code if my newWeight (weight to be set), is not equal to my current weight. Here is how I do it in case someone is curious: //Put in a LivingUpdateEvent. Simply set the player and my props for ease of access. /* Start update inventory */ InventoryPlayer inv = player.inventory; if(inv.inventoryChanged) { float currentWeight = props.getCurrentWeight(); float newWeight = 0; for(int i = 0; i < inv.getSizeInventory(); i++) { ItemStack item = inv.getStackInSlot(i); int multiplier = 1; if(item != null) { multiplier = item.stackSize; try { float weight = WeightLimitJson.getItemWeight(item.getItem()) * multiplier; newWeight += weight; }catch (IOException e) { e.printStackTrace(); } } } if(!player.worldObj.isRemote && newWeight != currentWeight) props.setWeight(newWeight); }
  9. Hm, alright. I'm using that now and it works quite well. Now, my other question is: is InventoryPlayer#inventoryChanged become true when anything enters or leaves the inventory? Or is it only in some cases?
  10. So those two events are slightly like RenderGameOverlayEvent.pre/post, correct?
  11. So, I am using the ItemPickupEvent to check for items, and add a weight amount to the player that is parsed from my json. This all works fine except for this: ItemStack item = event.pickedUp.getEntityItem(); int multiplier = item.stackSize; It seems as though "multiplier" always returns 0. I could of sworn in 1.7.10, I never had this issue. Is there a new way of getting the stackSize with this event?
  12. Well, I needed a mutable map that keeps the Objects in the order in which they were inserted. So, it all works now. I'll keep what you said in mind though, never realized how useful Maps are until now.
  13. How I do it now, is with a LinkedHashmap, that I fill after I sort my ArrayList of item registry name's. I watched a few videos explaining your way of doing it, and most said it is not needed unless you are organizing frequently.
  14. I didn't seem to understand what you meant last night Draco. Sorry again, and thank you!
  15. Alright, so I did a little more research (as I should of done before replying with my idiotic way of doing things, so I apologize), and I found that HashMaps will never save the Objects you put in them in a particular order (duh). But I found a neat little class called "LinkedHashMap", that will store the things you put in them, in the order that they enter. With this, I simply loop through the ItemRegistry, add the unsorted names to my List, then sort them. After that, I loop through my sorted list, grab the item from the name and get the weight value, then add it all to my LinkedHashMap (), and write it to json. Whoever is curious about this, here is my full code (NOTE: I create the file in my PreInit, and write to it in my post so I get every item): package com.happykiller.weightlimit.main.init.config; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.happykiller.weightlimit.api.IWeightedItem; import com.happykiller.weightlimit.main.ModMain; import net.minecraft.item.Item; import net.minecraftforge.fml.common.registry.FMLControlledNamespacedRegistry; import net.minecraftforge.fml.common.registry.GameData; public class WeightLimitJson { public static void writeJson() throws IOException { Gson json = new GsonBuilder().setPrettyPrinting().create(); Writer jsonFileWriter = new FileWriter(ModMain.configDirectory + "/WeightLimit.json"); FMLControlledNamespacedRegistry<Item> itemRegistry = GameData.getItemRegistry(); Map<String, Float> items = new LinkedHashMap<String, Float>(); List<String> itemsOrganized = new ArrayList<String>(); for(Item item : itemRegistry.typeSafeIterable()) { String name = itemRegistry.getNameForObject(item).toString(); itemsOrganized.add(name); } Collections.sort(itemsOrganized, new Comparator<String>() { public int compare(String modid1, String modid2) { return modid1.compareTo(modid2); } }); for(String string : itemsOrganized) { float weight; Item item = itemRegistry.getObject(string); if(item != null) { String name = itemRegistry.getNameForObject(item).toString(); System.out.println(name); if(item instanceof IWeightedItem) { weight = ((IWeightedItem)item).getItemWeight(); }else { weight = 0.5F; } items.put(name, weight); }else { System.out.println("ERROR: One or more of the items in the ArrayList were NULL!"); } } json.toJson(items, jsonFileWriter); jsonFileWriter.close(); } } I did not use TreeMap, because I do not need to constantly re-organize my Objects. I will only organize them once, during Post-Initialization. Thus, a LinkedHashMap with a List for Collection organizing I deem a bit easier. Thank you for all your guy's help! Next time, I'll only make one thread @Lex
  16. Alright, here is my new class. I have one problem, that I will explain. I hope I did remotely what you were saying I should, and I am sorry if I missed something. NOTE: I did not go with TreeMap yet as, I did not feel right abandoning this, and I sorta want to accomplish it this way so I have an understanding of how all this works. New Class: package com.happykiller.weightlimit.main.init.config; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.happykiller.weightlimit.api.IWeightedItem; import com.happykiller.weightlimit.main.ModMain; import net.minecraft.item.Item; import net.minecraftforge.fml.common.registry.FMLControlledNamespacedRegistry; import net.minecraftforge.fml.common.registry.GameData; import net.minecraftforge.fml.common.registry.GameRegistry; public class WeightLimitJson { public static void createJSON() throws IOException { Gson json = new GsonBuilder().setPrettyPrinting().create(); Writer jsonFileWriter = new FileWriter(ModMain.configDirectory + "/WeightLimit.json"); FMLControlledNamespacedRegistry<Item> itemRegistry = GameData.getItemRegistry(); Map<String, Float> items = new HashMap<String, Float>(); for(Item item : itemRegistry.typeSafeIterable()) { String name = itemRegistry.getNameForObject(item).toString(); items.put(name, 0.0F); //System.out.println(json.toJson(name).toString()); } List<String> itemsOrganized = new ArrayList<String>(items.keySet()); Collections.sort(itemsOrganized, new Comparator<String>() { public int compare(String modid1, String modid2) { return modid1.compareTo(modid2); } }); items.clear(); System.out.println("HASHMAP [sHOULD BE CLEARED]: " + items); System.out.println("ARRAYLIST: " + itemsOrganized); for(String string : itemsOrganized) { float weight; String[] modidID = string.split(":"); String modID = modidID[0]; String id = modidID[1]; Item item = GameRegistry.findItem(modID, id); if(item != null) { String name = itemRegistry.getNameForObject(item).toString(); if(item instanceof IWeightedItem) { weight = ((IWeightedItem)item).getItemWeight(); }else { weight = 0.5F; } items.put(name, weight); }else { System.out.println("ERROR: One or more of the items in the ArrayList were NULL!"); } } json.toJson(items, jsonFileWriter); System.out.println("Wrote Json"); jsonFileWriter.close(); } } So, I made it where at the start, I only write every item to the json, with a 0.0F value. Next, I organize it; then I clear my HashMap, and Iterate through my ArrayList for items. Now, I decided to have it print my HashMap after clearing, and my ArrayList. Here is what I got: NOTE: No need to read this, it shows my HashMap is clear, and my ArrayList ordered it correctly. So, I decide to fill my HashMap from my ArrayList or organized names, and run through my process of checking for my Interface. Works just fine. But then, I go and check my json, and get this: As you can see, it's all unorganized again! Excuse me if I got this wrong, but clearing and replacing the HashMap, would replace the index number of every line, correct? Again, I'm terribly sorry if I misunderstood what you said Draco.
  17. Sorry Lex, should of kept it in one thread. I'll post any more problems in this one, sorry again and thanks!
  18. @RANK I don't really understand what you mean, I sort them the best way I found: public static void createJSON() throws IOException { Gson json = new GsonBuilder().setPrettyPrinting().create(); Writer jsonFileWriter = new FileWriter(ModMain.configDirectory + "/WeightLimit.json"); FMLControlledNamespacedRegistry<Item> itemRegistry = GameData.getItemRegistry(); Map<String, Float> items = new HashMap<String, Float>(); for(Item item : itemRegistry.typeSafeIterable()) { String name = itemRegistry.getNameForObject(item).toString(); float weight; if(item instanceof IWeightedItem) { weight = ((IWeightedItem)item).getItemWeight(); }else { weight = 0.0F; } items.put(name, weight); //System.out.println(json.toJson(name).toString()); } List<String> itemsOrganized = new ArrayList<String>(items.keySet()); Collections.sort(itemsOrganized, new Comparator<String>() { public int compare(String modid1, String modid2) { return modid1.compareTo(modid2); } }); items.keySet().addAll(itemsOrganized); json.toJson(items, jsonFileWriter); System.out.println("Wrote Json"); jsonFileWriter.close(); } @Draco See, in the code posted above, I sort it and such; but it doesn't change my HashMap (But, I need to write the String(modid), and Float(weight) on the same line in the file. That's why I need to update the Map). I get why it doesn't, but I still can't organize my Map now considering the way I am doing it won't change it.
  19. Alright, hey, I'm back again with another silly question. After taking my Strings, organizing them using Collection, and printing them out; all I need to do is put them back in my HashMap. I've tried clearing the keySet(), and re-adding it from my list, didn't work. I tried using #addAll(), sadly, didn't work. Could someone point me in the right direction?
  20. I'm certainly off tonight... Map#keySet(). Thank you.
  21. So, I need to get the String from my HashMap into an ArrayList. Here's the problem, I can't find anywhere that get's the String, only the float. Here's an example: //This is my map Map<String, Float> items = new HashMap<String, Float>(); //I need to get all the Strings (modIDs) and set them in this list: List<String> itemsOrganized = new ArrayList<String>(items.?); //But, how do I get the string? Nothing in Map seems to get it, besides values (which gets my float) I am just lost on this. I have checked out docs, stackoverflow, java sites, everything. I just need to organize my Strings from my map. How do I do this?
  22. I found this, but I like to check with other people that have superior knowledge on the subject to see if there is anything more efficient to do. Thank you!
  23. Now see, I actually got into programming by modding; but I learned to research basic java before hand. Honestly though, I don't like people that think they are being insulted when someone tells them "You forgot to make it public, please learn Java". There is something right under this forum saying "Help with modding goes in here, however, please keep in mind that this is not a Java school. You are expected to have basic knowledge of Java before posting here." I'd say 80% to 90% of my smites came from people that I asked to learn Java. Anyway, I just want to thank you for your help with my issues time and time again, even if they seem a bit idiotic. Without you, and some others on this forum, I would be more frustrated then I should be.
  24. I was looking online and I found that I'd need to do this: Map<String, Person> people = new HashMap<String, Person>(); Person jim = new Person("Jim", 25); Person scott = new Person("Scott", 28); Person anna = new Person("Anna", 23); people.put(jim.getName(), jim); people.put(scott.getName(), scott); people.put(anna.getName(), anna); // not yet sorted List<Person> peopleByAge = new ArrayList<Person>(people.values()); Collections.sort(peopleByAge, new Comparator<Person>() { public int compare(Person o1, Person o2) { return o1.getAge() - o2.getAge(); } }); I need to make a Map, add my string (mod id) to a List, and sort them with a Comparator?
  25. So, I am using a HashMap to write things to a json file. It works, but the problem is nothing is organized. I know it's a small thing, but it can have a huge impact on the person attempting to edit it. How would I organize my HashMap alphabetically? Here is my writer (called in my postInit): package com.happykiller.weightlimit.main.init.config; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.util.HashMap; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.happykiller.weightlimit.api.IWeightedItem; import com.happykiller.weightlimit.main.ModMain; import net.minecraft.item.Item; import net.minecraftforge.fml.common.registry.FMLControlledNamespacedRegistry; import net.minecraftforge.fml.common.registry.GameData; public class WeightLimitJson { private String name; private float weightValue; public static void createJSON() throws IOException { Gson json = new GsonBuilder().setPrettyPrinting().create(); Writer jsonFileWriter = new FileWriter(ModMain.configDirectory + "/WeightLimit.json"); FMLControlledNamespacedRegistry<Item> itemRegistry = GameData.getItemRegistry(); HashMap<String, Float> items = new HashMap<String, Float>(); for(Item item : itemRegistry.typeSafeIterable()) { String name = itemRegistry.getNameForObject(item).toString(); float weight; if(item instanceof IWeightedItem) { weight = ((IWeightedItem)item).getItemWeight(); }else { weight = 0.0F; } items.put(name, weight); //System.out.println(json.toJson(name).toString()); } json.toJson(items, jsonFileWriter); jsonFileWriter.close(); } }
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.