<commit_before>/*
 * Copyright (c) 2014, DoubleDoorDevelopment
 * 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 project 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.d3core.util;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.EventPriority;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.registry.GameData;
import net.doubledoordev.d3core.D3Core;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerDropsEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import org.apache.commons.io.IOUtils;

import java.net.URL;
import java.nio.charset.Charset;

/**
 * Something other than capes for once
 *
 * @author Dries007
 */
public class DevPerks
{
    private JsonObject  perks = new JsonObject();

    public DevPerks()
    {
        try
        {
            perks = new JsonParser().parse(IOUtils.toString(new URL(CoreConstants.PERKSURL), Charset.forName("UTF-8"))).getAsJsonObject();
        }
        catch (Exception e)
        {
            if (D3Core.debug()) e.printStackTrace();
        }
    }

    /**
     * Something other than capes for once
     */
    @SubscribeEvent
    public void nameFormatEvent(PlayerEvent.NameFormat event)
    {
        try
        {
            if (D3Core.debug()) perks = new JsonParser().parse(IOUtils.toString(new URL(CoreConstants.PERKSURL), Charset.forName("UTF-8"))).getAsJsonObject();
            if (perks.has(event.username))
            {
                JsonObject perk = perks.getAsJsonObject(event.username);
                if (perk.has("displayname")) event.displayname = perk.get("displayname").getAsString();
                if (perk.has("hat") && (event.entityPlayer.inventory.armorInventory[3] == null || event.entityPlayer.inventory.armorInventory[3].stackSize == 0))
                {
                    JsonObject hat = perk.getAsJsonObject("hat");
                    String name = hat.get("name").getAsString();
                    int meta = hat.has("meta") ? hat.get("meta").getAsInt() : 0;
                    event.entityPlayer.inventory.armorInventory[3] = new ItemStack(GameData.getItemRegistry().getObject(name), 0, meta);
                }
            }
        }
        catch (Exception e)
        {
            if (D3Core.debug()) e.printStackTrace();
        }
    }

    @SubscribeEvent
    public void cloneEvent(PlayerEvent.Clone event)
    {
        try
        {
            if (D3Core.debug()) perks = new JsonParser().parse(IOUtils.toString(new URL(CoreConstants.PERKSURL), Charset.forName("UTF-8"))).getAsJsonObject();
            if (perks.has(event.original.getCommandSenderName()))
            {
                JsonObject perk = perks.getAsJsonObject(event.original.getCommandSenderName());
                if (perk.has("hat") && (event.entityPlayer.inventory.armorInventory[3] == null || event.entityPlayer.inventory.armorInventory[3].stackSize == 0))
                {
                    JsonObject hat = perk.getAsJsonObject("hat");
                    String name = hat.get("name").getAsString();
                    int meta = hat.has("meta") ? hat.get("meta").getAsInt() : 0;
                    event.entityPlayer.inventory.armorInventory[3] = new ItemStack(GameData.getItemRegistry().getObject(name), 0, meta);
                }
            }
        }
        catch (Exception e)
        {
            if (D3Core.debug()) e.printStackTrace();
        }
    }

    @SubscribeEvent(priority = EventPriority.HIGHEST)
    public void deathEvent(PlayerDropsEvent event)
    {
        try
        {
            if (D3Core.debug()) perks = new JsonParser().parse(IOUtils.toString(new URL(CoreConstants.PERKSURL), Charset.forName("UTF-8"))).getAsJsonObject();
            if (perks.has(event.entityPlayer.getCommandSenderName()))
            {
                JsonObject perk = perks.getAsJsonObject(event.entityPlayer.getCommandSenderName());
                if (perk.has("drop"))
                {
                    JsonObject drop = perk.getAsJsonObject("drop");
                    String name = drop.get("name").getAsString();
                    int meta = drop.has("meta") ? drop.get("meta").getAsInt() : 0;
                    int size = drop.has("size") ? drop.get("size").getAsInt() : 1;
                    event.drops.add(new EntityItem(event.entityPlayer.getEntityWorld(), event.entityPlayer.posX, event.entityPlayer.posY, event.entityPlayer.posZ, new ItemStack(GameData.getItemRegistry().getObject(name), size, meta)));
                }
            }
        }
        catch (Exception e)
        {
            if (D3Core.debug()) e.printStackTrace();
        }
    }

    public void update(boolean sillyness)
    {
        try<commit_msg>devperks update<commit_after>/*
 * Copyright (c) 2014, DoubleDoorDevelopment
 * 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 project 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.d3core.util;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.EventPriority;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.registry.GameData;
import net.doubledoordev.d3core.D3Core;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerDropsEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import org.apache.commons.io.IOUtils;

import java.net.URL;
import java.nio.charset.Charset;

/**
 * Something other than capes for once
 *
 * @author Dries007
 */
public class DevPerks
{
    private JsonObject  perks = new JsonObject();

    public DevPerks()
    {
        try
        {
            perks = new JsonParser().parse(IOUtils.toString(new URL(CoreConstants.PERKSURL), Charset.forName("UTF-8"))).getAsJsonObject();
        }
        catch (Exception e)
        {
            if (D3Core.debug()) e.printStackTrace();
        }
    }

    public static ItemStack getItemStackFromJson(JsonObject data, int defaultMeta, int defaultStacksize)
    {
        int meta = data.has("meta") ? data.get("meta").getAsInt() : defaultMeta;
        int size = data.has("size") ? data.get("size").getAsInt() : defaultStacksize;
        ItemStack stack = new ItemStack(GameData.getItemRegistry().getObject(data.get("name").getAsString()), size, meta);
        if (data.has("display")) stack.setStackDisplayName(data.get("display").getAsString());
        if (data.has("color"))
        {
            NBTTagCompound root = stack.getTagCompound();
            if (root == null) root = new NBTTagCompound();
            NBTTagCompound display = root.getCompoundTag("display");
            display.setInteger("color", data.get("color").getAsInt());
            root.setTag("display", display);
            stack.setTagCompound(root);
        }
        if (data.has("lore"))
        {
            NBTTagCompound root = stack.getTagCompound();
            if (root == null) root = new NBTTagCompound();
            NBTTagCompound display = root.getCompoundTag("display");
            NBTTagList lore = new NBTTagList();
            for (JsonElement element : data.getAsJsonArray("lore")) lore.appendTag(new NBTTagString(element.getAsString()));
            display.setTag("Lore", lore);
            root.setTag("display", display);
            stack.setTagCompound(root);
        }
        return stack;
    }

    /**
     * Something other than capes for once
     */
    @SubscribeEvent
    public void nameFormatEvent(PlayerEvent.NameFormat event)
    {
        try
        {
            if (D3Core.debug()) perks = new JsonParser().parse(IOUtils.toString(new URL(CoreConstants.PERKSURL), Charset.forName("UTF-8"))).getAsJsonObject();
            if (perks.has(event.username))
            {
                JsonObject perk = perks.getAsJsonObject(event.username);
                if (perk.has("displayname")) event.displayname = perk.get("displayname").getAsString();
                if (perk.has("hat") && (event.entityPlayer.inventory.armorInventory[3] == null || event.entityPlayer.inventory.armorInventory[3].stackSize == 0))
                {
                    ItemStack hat = getItemStackFromJson(perk.getAsJsonObject("hat"), 0, 0);
                    hat.stackSize = 0;
                    event.entityPlayer.inventory.armorInventory[3] = hat;
                }
            }
        }
        catch (Exception e)
        {
            if (D3Core.debug()) e.printStackTrace();
        }
    }

    @SubscribeEvent
    public void cloneEvent(PlayerEvent.Clone event)
    {
        try
        {
            if (D3Core.debug()) perks = new JsonParser().parse(IOUtils.toString(new URL(CoreConstants.PERKSURL), Charset.forName("UTF-8"))).getAsJsonObject();
            if (perks.has(event.original.getCommandSenderName()))
            {
                JsonObject perk = perks.getAsJsonObject(event.original.getCommandSenderName());
                if (perk.has("hat") && (event.entityPlayer.inventory.armorInventory[3] == null || event.entityPlayer.inventory.armorInventory[3].stackSize == 0))
                {
                    ItemStack hat = getItemStackFromJson(perk.getAsJsonObject("hat"), 0, 0);
                    hat.stackSize = 0;
                    event.entityPlayer.inventory.armorInventory[3] = hat;
                }
            }
        }
        catch (Exception e)
        {
            if (D3Core.debug()) e.printStackTrace();
        }
    }

    @SubscribeEvent(priority = EventPriority.HIGHEST)
    public void deathEvent(PlayerDropsEvent event)
    {
        try
        {
            if (D3Core.debug()) perks = new JsonParser().parse(IOUtils.toString(new URL(CoreConstants.PERKSURL), Charset.forName("UTF-8"))).getAsJsonObject();
            if (perks.has(event.entityPlayer.getCommandSenderName()))
            {
                JsonObject perk = perks.getAsJsonObject(event.entityPlayer.getCommandSenderName());
                if (perk.has("drop"))
                {
                    event.drops.add(new EntityItem(event.entityPlayer.getEntityWorld(), event.entityPlayer.posX, event.entityPlayer.posY, event.entityPlayer.posZ, getItemStackFromJson(perk.getAsJsonObject("drop"), 0, 1)));
                }
            }
        }
        catch (Exception e)
        {
            if (D3Core.debug()) e.printStackTrace();
        }
    }

    public void update(boolean sillyness)
    {
        try
{'fraction_non_alphanumeric': 0.10781181936982721, 'fraction_numerical': 0.005227239727021925, 'mean_word_length': 2.6752934898612595, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1369104', 'n_tokens_mistral': 3979, 'n_tokens_neox': 3822, 'n_words': 937}