Skip to content
Permalink
Browse files

fixed #1 + reformat

  • Loading branch information...
dries007 committed Aug 3, 2015
1 parent 382215c commit c84ecf02eb10a97fc2d5e8edf6c2a5553be2a771
@@ -35,7 +35,6 @@
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.FMLServerStartedEvent;
import cpw.mods.fml.common.event.FMLServerStartingEvent;
import cpw.mods.fml.common.registry.GameRegistry;
import net.doubledoordev.jsonlootbags.items.ItemBag;
@@ -53,8 +52,8 @@
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.List;
import java.util.Random;

import static net.minecraftforge.common.ChestGenHooks.*;

@@ -66,9 +65,8 @@
{
@Mod.Instance(Constants.MODID)
public static JsonLootBags instance;

private File folder;
public ItemBag bag;
private File folder;
private boolean makeexamples;

@Mod.EventHandler
@@ -93,7 +91,7 @@ public void postInit(FMLPostInitializationEvent event) throws IOException, NoSuc
Random random = new Random();
Field contents = ChestGenHooks.class.getDeclaredField("contents");
contents.setAccessible(true);
for (String name : new String[] {DUNGEON_CHEST, BONUS_CHEST, VILLAGE_BLACKSMITH, STRONGHOLD_CROSSING, STRONGHOLD_LIBRARY, STRONGHOLD_CORRIDOR, PYRAMID_JUNGLE_DISPENSER, PYRAMID_JUNGLE_CHEST, PYRAMID_DESERT_CHEST, MINESHAFT_CORRIDOR})
for (String name : new String[]{DUNGEON_CHEST, BONUS_CHEST, VILLAGE_BLACKSMITH, STRONGHOLD_CROSSING, STRONGHOLD_LIBRARY, STRONGHOLD_CORRIDOR, PYRAMID_JUNGLE_DISPENSER, PYRAMID_JUNGLE_CHEST, PYRAMID_DESERT_CHEST, MINESHAFT_CORRIDOR})
{
//noinspection unchecked
List<WeightedRandomChestContent> list = (List<WeightedRandomChestContent>) contents.get(ChestGenHooks.getInfo(name));
@@ -60,14 +60,6 @@ public ItemBag()
setHasSubtypes(true);
}

@Override
public String getItemStackDisplayName(ItemStack stack)
{
BagType bagType = BagType.getFromStack(stack);
if (bagType == null) return "Invalid loot bag!";
return bagType.itemname;
}

@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
{
@@ -84,17 +76,6 @@ public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer pla
return stack;
}

@SuppressWarnings("unchecked")
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(Item item, CreativeTabs tab, List list)
{
for (BagType type : BagType.getAllTypes())
{
list.add(type.getBagStack());
}
}

@Override
@SideOnly(Side.CLIENT)
public int getColorFromItemStack(ItemStack stack, int pass)
@@ -104,11 +85,30 @@ public int getColorFromItemStack(ItemStack stack, int pass)
return bagType.color.getRGB();
}

@Override
public String getItemStackDisplayName(ItemStack stack)
{
BagType bagType = BagType.getFromStack(stack);
if (bagType == null) return "Invalid loot bag!";
return bagType.itemname;
}

@Override
public EnumRarity getRarity(ItemStack stack)
{
BagType bagType = BagType.getFromStack(stack);
if (bagType == null) return EnumRarity.common;
return bagType.rarity;
}

@SuppressWarnings("unchecked")
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(Item item, CreativeTabs tab, List list)
{
for (BagType type : BagType.getAllTypes())
{
list.add(type.getBagStack());
}
}
}
@@ -35,11 +35,9 @@
import net.doubledoordev.jsonlootbags.JsonLootBags;
import net.minecraft.inventory.InventoryBasic;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.WeightedRandomChestContent;
import net.minecraftforge.common.ChestGenHooks;

import java.awt.*;
import java.util.*;
@@ -52,37 +50,37 @@
{
private static final Map<String, BagType> bagTypeMap = new HashMap<>();

private String name;
public String itemname;
public Color color;
public int invSlotsMin = 36;
public int invSlotsMax = 36;
public int amountOfItemsMin = 1;
public int amountOfItemsMax = 10;
public WeightedRandomChestContent[] items;
// public Map<String, MinMaxWeight> asLoot = new HashMap<>();
public EnumRarity rarity = EnumRarity.common;
private String name;

private void init()
public static BagType getFromStack(ItemStack stack)
{
// for (Map.Entry<String, MinMaxWeight> entry : asLoot.entrySet())
// {
// ChestGenHooks chestGenHooks = ChestGenHooks.getInfo(entry.getKey());
// MinMaxWeight mmw = entry.getValue();
// chestGenHooks.addItem(new WeightedRandomChestContent(JsonLootBags.instance.bag, 0, mmw.min, mmw.max, mmw.weight));
// }
NBTTagCompound tagCompound = stack.stackTagCompound;
if (tagCompound == null) return null;
return bagTypeMap.get(tagCompound.getString("type"));
}

public String getName()
public static void register(String name, BagType bagType)
{
return name;
bagType.name = name;
bagTypeMap.put(name, bagType);
}

public static BagType getFromStack(ItemStack stack)
public static Iterable<? extends BagType> getAllTypes()
{
NBTTagCompound tagCompound = stack.stackTagCompound;
if (tagCompound == null) return null;
return bagTypeMap.get(tagCompound.getString("type"));
return bagTypeMap.values();
}

public String getName()
{
return name;
}

public ItemStack getBagStack()
@@ -93,13 +91,6 @@ public ItemStack getBagStack()
return stack;
}

public static void register(String name, BagType bagType)
{
bagType.name = name;
bagType.init();
bagTypeMap.put(name, bagType);
}

public List<ItemStack> getRandomItems(Random rand)
{
InventoryBasic inventory = new InventoryBasic("", false, Helper.randInt(rand, invSlotsMin, invSlotsMax));
@@ -113,11 +104,6 @@ public static void register(String name, BagType bagType)
return list;
}

public static Iterable<? extends BagType> getAllTypes()
{
return bagTypeMap.values();
}

public static class MinMaxWeight
{
public int min, max, weight;
@@ -34,10 +34,8 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.doubledoordev.jsonlootbags.util.jsonparsing.JsonColor;
import net.doubledoordev.jsonlootbags.util.jsonparsing.JsonItemStack;
import net.doubledoordev.jsonlootbags.util.jsonparsing.JsonNBT;
import net.doubledoordev.jsonlootbags.util.jsonparsing.JsonWeightedRandomChestContent;
import net.doubledoordev.jsonlootbags.util.jsonparsing.*;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.util.WeightedRandomChestContent;
@@ -56,5 +54,6 @@
.registerTypeHierarchyAdapter(WeightedRandomChestContent.class, new JsonWeightedRandomChestContent())
.registerTypeHierarchyAdapter(ItemStack.class, new JsonItemStack())
.registerTypeHierarchyAdapter(NBTBase.class, new JsonNBT())
.registerTypeHierarchyAdapter(EnumRarity.class, new JsonEnumRarity())
.create();
}
@@ -35,16 +35,13 @@
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@@ -65,18 +62,6 @@ public String getCommandUsage(ICommandSender p_71518_1_)
return "'/jsonlootbags dumpNBT' to dump what you are holding.";
}

@Override
public boolean canCommandSenderUseCommand(ICommandSender p_71519_1_)
{
return p_71519_1_ instanceof EntityPlayer && (MinecraftServer.getServer().isSinglePlayer() || MinecraftServer.getServer().getConfigurationManager().func_152596_g(((EntityPlayer) p_71519_1_).getGameProfile()));
}

@Override
public List addTabCompletionOptions(ICommandSender p_71516_1_, String[] p_71516_2_)
{
return getListOfStringsMatchingLastWord(p_71516_2_, "dumpNBT");
}

@Override
public void processCommand(ICommandSender p_71515_1_, String[] p_71515_2_)
{
@@ -87,14 +72,26 @@ public void processCommand(ICommandSender p_71515_1_, String[] p_71515_2_)
if (stack == null) throw new CommandException("You aren't holding anything.");

FileUtils.write(new File("JsonLootBags-NBTdump.txt"), "// Dump from " + p_71515_1_.getCommandSenderName() +
"\n// Item name: " + stack.getDisplayName() +
"\n// Timestamp: " + new Date().toString() +
'\n' + Constants.GSON.toJson(stack) + '\n', true);
"\n// Item name: " + stack.getDisplayName() +
"\n// Timestamp: " + new Date().toString() +
'\n' + Constants.GSON.toJson(stack) + '\n', true);
}
catch (Exception e)
{
e.printStackTrace();
throw new CommandException(e.getMessage());
}
}

@Override
public boolean canCommandSenderUseCommand(ICommandSender p_71519_1_)
{
return p_71519_1_ instanceof EntityPlayer && (MinecraftServer.getServer().isSinglePlayer() || MinecraftServer.getServer().getConfigurationManager().func_152596_g(((EntityPlayer) p_71519_1_).getGameProfile()));
}

@Override
public List addTabCompletionOptions(ICommandSender p_71516_1_, String[] p_71516_2_)
{
return getListOfStringsMatchingLastWord(p_71516_2_, "dumpNBT");
}
}
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2014,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the {organization} nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/

package net.doubledoordev.jsonlootbags.util.jsonparsing;

import com.google.gson.*;
import net.minecraft.item.EnumRarity;

import java.lang.reflect.Type;

/**
* @author Dries007
*/
public class JsonEnumRarity implements JsonSerializer<EnumRarity>, JsonDeserializer<EnumRarity>
{
@Override
public EnumRarity deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
String name = json.getAsString();
for (EnumRarity rarity : EnumRarity.values()) if (rarity.rarityName.equalsIgnoreCase(name) || rarity.name().equalsIgnoreCase(name)) return rarity;
return EnumRarity.common;
}

@Override
public JsonElement serialize(EnumRarity src, Type typeOfSrc, JsonSerializationContext context)
{
return new JsonPrimitive(src.rarityName);
}
}
@@ -44,7 +44,9 @@
*/
public class JsonItemStack implements JsonSerializer<ItemStack>, JsonDeserializer<ItemStack>
{
public static final Type TYPE = new TypeToken<ItemStack>(){}.getType();
public static final Type TYPE = new TypeToken<ItemStack>()
{
}.getType();

@Override
public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
@@ -44,7 +44,9 @@
*/
public class JsonNBT implements JsonSerializer<NBTBase>, JsonDeserializer<NBTBase>
{
public static final Type TYPE = new TypeToken<NBTBase>() {}.getType();
public static final Type TYPE = new TypeToken<NBTBase>()
{
}.getType();

@Override
public NBTBase deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
@@ -117,7 +119,7 @@ else if (!string.equalsIgnoreCase("true") && !string.equalsIgnoreCase("false"))
{
if (astring.length <= 1)
{
return new NBTTagIntArray(new int[] {Integer.parseInt(s.trim())});
return new NBTTagIntArray(new int[]{Integer.parseInt(s.trim())});
}
else
{
@@ -154,7 +156,7 @@ else if (!string.equalsIgnoreCase("true") && !string.equalsIgnoreCase("false"))
}
else
{
return new NBTTagByte((byte)(Boolean.parseBoolean(string) ? 1 : 0));
return new NBTTagByte((byte) (Boolean.parseBoolean(string) ? 1 : 0));
}
}
throw new JsonParseException("Invalid NBT: " + json.toString());

0 comments on commit c84ecf0

Please sign in to comment.
You can’t perform that action at this time.