Skip to content

Commit

Permalink
Start of configurable blacklists for right click harvesting, plant dr…
Browse files Browse the repository at this point in the history
…ops, and food value modification
  • Loading branch information
squeek502 committed Dec 27, 2014
1 parent 8b32844 commit 1ea9d31
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 14 deletions.
17 changes: 13 additions & 4 deletions src/main/java/iguanaman/hungeroverhaul/HungerOverhaul.java
Expand Up @@ -24,10 +24,12 @@
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLInterModComms;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import iguanaman.hungeroverhaul.food.FoodModifier;
import iguanaman.hungeroverhaul.potion.PotionWellFed;
import iguanaman.hungeroverhaul.util.ComponentVillageCustomField;
import iguanaman.hungeroverhaul.util.IMCHandler;
import iguanaman.hungeroverhaul.util.IguanaEventHook;
import iguanaman.hungeroverhaul.util.RecipeRemover;
import iguanaman.hungeroverhaul.util.ItemTweaks;
Expand All @@ -42,6 +44,7 @@ public class HungerOverhaul
public static HungerOverhaul instance;

public static Potion potionWellFed;

@EventHandler
public void preInit(FMLPreInitializationEvent event)
{
Expand Down Expand Up @@ -69,16 +72,16 @@ public void init(FMLInitializationEvent event)
MinecraftForge.EVENT_BUS.register(new ModulePlantGrowth());
MinecraftForge.EVENT_BUS.register(new ModuleBonemeal());
ModuleVanilla.init();
if(Loader.isModLoaded("harvestcraft"))
if (Loader.isModLoaded("harvestcraft"))
{
PamsModsHelper.loadHC();
ModuleHarvestCraft.init();
}
if(Loader.isModLoaded("temperateplants"))
if (Loader.isModLoaded("temperateplants"))
ModuleTemperatePlants.init();
if(Loader.isModLoaded("randomplants"))
if (Loader.isModLoaded("randomplants"))
ModuleRandomPlants.init();
if(Loader.isModLoaded("weeeflowers"))
if (Loader.isModLoaded("weeeflowers"))
{
PamsModsHelper.loadWF();
ModuleWeeeFlowers.init();
Expand Down Expand Up @@ -106,4 +109,10 @@ public void postInit(FMLPostInitializationEvent event)
MinecraftForge.EVENT_BUS.register(new IguanaEventHook());
FMLCommonHandler.instance().bus().register(new ModuleRespawnHunger());
}

@EventHandler
public void handleIMCMessages(FMLInterModComms.IMCEvent event)
{
IMCHandler.processMessages(event.getMessages());
}
}
13 changes: 9 additions & 4 deletions src/main/java/iguanaman/hungeroverhaul/food/FoodModifier.java
@@ -1,6 +1,7 @@
package iguanaman.hungeroverhaul.food;

import iguanaman.hungeroverhaul.config.IguanaConfig;
import iguanaman.hungeroverhaul.util.ItemAndBlockList;
import squeek.applecore.api.food.FoodEvent;
import squeek.applecore.api.food.FoodValues;

Expand All @@ -15,8 +16,9 @@
public class FoodModifier
{
private static HashMap<ItemStack, FoodValues> modifiedFoodValues = new HashMap<ItemStack, FoodValues>();
public static ItemAndBlockList blacklist = new ItemAndBlockList();

@SubscribeEvent(priority=EventPriority.HIGHEST)
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void getModifiedFoodValues(FoodEvent.GetFoodValues event)
{
if (!IguanaConfig.modifyFoodValues)
Expand Down Expand Up @@ -45,11 +47,14 @@ public static void setModifiedFoodValues(ItemStack stack, FoodValues values)

private static FoodValues lookupModifiedFoodValues(ItemStack stack)
{
for (Map.Entry<ItemStack, FoodValues> entry : modifiedFoodValues.entrySet())
if (!blacklist.contains(stack))
{
if (stack.isItemEqual(entry.getKey()))
for (Map.Entry<ItemStack, FoodValues> entry : modifiedFoodValues.entrySet())
{
return entry.getValue();
if (stack.isItemEqual(entry.getKey()))
{
return entry.getValue();
}
}
}
return null;
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/iguanaman/hungeroverhaul/util/IMCHandler.java
@@ -0,0 +1,76 @@
package iguanaman.hungeroverhaul.util;

import iguanaman.hungeroverhaul.HungerOverhaul;
import iguanaman.hungeroverhaul.food.FoodModifier;
import net.minecraft.block.Block;
import net.minecraft.item.Item;

import com.google.common.collect.ImmutableList;

import cpw.mods.fml.common.event.FMLInterModComms;
import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage;
import cpw.mods.fml.common.registry.GameData;

public class IMCHandler
{
public static String BLACKLIST_RIGHT_CLICK = "BlacklistRightClick";
public static String BLACKLIST_DROPS = "BlacklistDrops";
public static String BLACKLIST_FOOD = "BlacklistFood";

public static void processMessages(ImmutableList<IMCMessage> messages)
{
for (final FMLInterModComms.IMCMessage message : messages)
{
ItemAndBlockList blacklist = null;

if (message.key.equalsIgnoreCase(BLACKLIST_RIGHT_CLICK))
{
blacklist = IguanaEventHook.rightClickHarvestBlacklist;
}
else if (message.key.equalsIgnoreCase(BLACKLIST_DROPS))
{
blacklist = IguanaEventHook.harvestDropsBlacklist;
}
else if (message.key.equalsIgnoreCase(BLACKLIST_FOOD))
{
blacklist = FoodModifier.blacklist;
}

if (blacklist != null)
{
if (message.isItemStackMessage())
{
blacklist.add(message.getItemStackValue());
}
else if (message.isStringMessage())
{
String msg = message.getStringValue();
if (msg.contains(":"))
{
Item item = GameData.getItemRegistry().getObject(msg);
Block block = GameData.getBlockRegistry().getObject(msg);

if (item != null)
blacklist.add(item);
if (block != null)
blacklist.add(block);
}
else
{
try
{
Class<?> clazz = Class.forName(message.getStringValue());
blacklist.add(clazz);
}
catch (ClassNotFoundException e)
{
HungerOverhaul.Log.error("Class to blacklist not found (sent by mod " + message.getSender() + ")");
e.printStackTrace();
}
}
}
}
}
}

}
23 changes: 17 additions & 6 deletions src/main/java/iguanaman/hungeroverhaul/util/IguanaEventHook.java
Expand Up @@ -52,6 +52,21 @@

public class IguanaEventHook
{
private static long lastRightClickCrop = 0;
public static ItemAndBlockList rightClickHarvestBlacklist = new ItemAndBlockList();
public static ItemAndBlockList harvestDropsBlacklist = new ItemAndBlockList();
static
{
if (Loader.isModLoaded("ExtraUtilities"))
{
Block enderLilly = Block.getBlockFromName("ExtraUtilities:plant/ender_lilly");
if (enderLilly != null)
{
rightClickHarvestBlacklist.add(enderLilly);
harvestDropsBlacklist.add(enderLilly);
}
}
}

@SubscribeEvent
public void onLivingUpdate(LivingUpdateEvent event)
Expand Down Expand Up @@ -299,8 +314,6 @@ public void onLivingHurtEvent(LivingHurtEvent event)
}
}

static long lastRightClickCrop = 0;

@SubscribeEvent
public void onPlayerInteraction(PlayerInteractEvent event)
{
Expand Down Expand Up @@ -343,8 +356,7 @@ public void onPlayerInteraction(PlayerInteractEvent event)
int resultingMeta = -1;

// certain things we don't want to add right-click harvest support for
// TODO: allow the user to set a custom blacklist?
if (clicked == Block.getBlockFromName("ExtraUtilities:plant/ender_lilly"))
if (rightClickHarvestBlacklist.contains(clicked))
return;

if (Loader.isModLoaded("Natura") && clicked instanceof CropBlock)
Expand Down Expand Up @@ -391,8 +403,7 @@ else if (clicked instanceof BlockCrops && meta >= 7)
public void onBlockHarvested(BlockEvent.HarvestDropsEvent event)
{
// certain things we don't want to modify the drops of
// TODO: allow the user to set a custom blacklist?
if (event.block == Block.getBlockFromName("ExtraUtilities:plant/ender_lilly"))
if (harvestDropsBlacklist.contains(event.block))
return;

boolean isNaturaCrop = Loader.isModLoaded("Natura") && event.block instanceof CropBlock;
Expand Down
97 changes: 97 additions & 0 deletions src/main/java/iguanaman/hungeroverhaul/util/ItemAndBlockList.java
@@ -0,0 +1,97 @@
package iguanaman.hungeroverhaul.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;

public class ItemAndBlockList
{
private static LinkedHashSet<Class<?>> classes = new LinkedHashSet<Class<?>>();
private static HashSet<Item> items = new HashSet<Item>();
private static HashSet<Block> blocks = new HashSet<Block>();
private static List<ItemStack> itemStacks = new ArrayList<ItemStack>();

public void add(Class<?> clazz)
{
classes.add(clazz);
}

public void add(Item item)
{
items.add(item);
}

public void add(Block block)
{
blocks.add(block);
}

public void add(ItemStack itemStack)
{
itemStacks.add(itemStack);
}

public boolean contains(Class<?> clazz)
{
if (classes.contains(clazz))
return true;

Iterator<Class<?>> itr = classes.iterator();
while (itr.hasNext())
{
Class<?> testClass = itr.next();
if (testClass.isAssignableFrom(clazz))
return true;
}

return false;
}

public boolean contains(ItemStack itemStack)
{
for (ItemStack curItemStack : itemStacks)
{
if (OreDictionary.itemMatches(curItemStack, itemStack, false))
return true;
}
return false;
}

public boolean contains(Item item)
{
if (items.contains(item))
return true;

if (contains(new ItemStack(item)))
return true;

if (contains(item.getClass()))
return true;

return false;
}

public boolean contains(Block block)
{
if (blocks.contains(block))
return true;

if (contains(new ItemStack(block)))
return true;

if (contains(block.getClass()))
return true;

if (contains(Item.getItemFromBlock(block)))
return true;

return false;
}
}

0 comments on commit 1ea9d31

Please sign in to comment.