refactor match handler example generation, add tool handling, minor tweaks and fixes

This commit is contained in:
Adrian 2015-08-07 13:23:47 +02:00
parent 404510b4f1
commit 64ecdd82bd
15 changed files with 161 additions and 57 deletions

View file

@ -0,0 +1,23 @@
package buildcraft.api.lists;
import java.util.List;
import net.minecraft.item.ItemStack;
public abstract class ListMatchHandler {
public enum Type {
TYPE, MATERIAL, CLASS
}
public abstract boolean matches(Type type, ItemStack stack, ItemStack target, boolean precise);
public abstract boolean isValidSource(Type type, ItemStack stack);
/**
* Get custom client examples.
* @param type
* @param stack
* @return A List (even empty!) if the examples satisfy this handler, null if iteration and .matches should be used instead.
*/
public List<ItemStack> getClientExamples(Type type, ItemStack stack) {
return null;
}
}

View file

@ -1,10 +1,13 @@
package buildcraft.core.list;
package buildcraft.api.lists;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.minecraft.item.Item;
public final class ListRegistry {
public static final List<Class<? extends Item>> itemClassAsType = new ArrayList<Class<? extends Item>>();
private static final List<ListMatchHandler> handlers = new ArrayList<ListMatchHandler>();
private ListRegistry() {

View file

@ -0,0 +1,12 @@
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* The BuildCraft API is distributed under the terms of the MIT License.
* Please check the contents of the license, which should be located
* as "LICENSE.API" in the BuildCraft source code distribution.
*/
@API(apiVersion = "1.0", owner = "BuildCraftAPI|core", provides = "BuildCraftAPI|lists")
package buildcraft.api.lists;
import cpw.mods.fml.common.API;

View file

@ -0,0 +1,9 @@
Improvements:
* Lists
* Lists now accept empty Lists for sorting (asie)
* Support for sorting tools by type (asie)
Bugs fixed:
* Hovering over slot causing incorrect item lighting (asie)

View file

@ -131,8 +131,9 @@ import buildcraft.core.lib.utils.XorShift128Random;
import buildcraft.core.list.ListMatchHandlerClass;
import buildcraft.core.list.ListMatchHandlerFluid;
import buildcraft.core.list.ListMatchHandlerOreDictionary;
import buildcraft.core.list.ListMatchHandlerTools;
import buildcraft.core.list.ListOreDictionaryCache;
import buildcraft.core.list.ListRegistry;
import buildcraft.api.lists.ListRegistry;
import buildcraft.core.list.ListTooltipHandler;
import buildcraft.core.network.PacketHandlerCore;
import buildcraft.core.properties.WorldPropertyIsDirt;
@ -466,7 +467,7 @@ public class BuildCraftCore extends BuildCraftMod {
FillerManager.registry.addPattern(new PatternCylinder());
FillerManager.registry.addPattern(new PatternFrame());
} catch (Error error) {
BCLog.logErrorAPI("Buildcraft", error, IFillerPattern.class);
BCLog.logErrorAPI(error, IFillerPattern.class);
throw error;
}
@ -477,12 +478,12 @@ public class BuildCraftCore extends BuildCraftMod {
ListRegistry.registerHandler(new ListMatchHandlerClass());
ListRegistry.registerHandler(new ListMatchHandlerFluid());
ListMatchHandlerClass.itemClasses.add(ItemFood.class);
ListRegistry.registerHandler(new ListMatchHandlerTools());
ListRegistry.itemClassAsType.add(ItemFood.class);
}
@Mod.EventHandler
public void postInit(FMLPostInitializationEvent event) {
OreDictionary.registerOre("dustTinyRedstone", Blocks.fence_gate);
BCLog.logger.info("BuildCraft's fake player: UUID = " + gameProfile.getId().toString() + ", name = '" + gameProfile.getName() + "'!");
for (Object o : Block.blockRegistry) {

View file

@ -37,6 +37,7 @@ import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.MinecraftForge;
import buildcraft.api.boards.RedstoneBoardRegistry;
import buildcraft.api.lists.ListRegistry;
import buildcraft.api.recipes.BuildcraftRecipeRegistry;
import buildcraft.api.robots.RobotManager;
import buildcraft.api.statements.IActionInternal;
@ -49,7 +50,6 @@ import buildcraft.core.DefaultProps;
import buildcraft.core.InterModComms;
import buildcraft.core.Version;
import buildcraft.core.config.ConfigManager;
import buildcraft.core.list.ListMatchHandlerClass;
import buildcraft.core.network.EntityIds;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.robotics.BlockRequester;
@ -340,7 +340,7 @@ public class BuildCraftRobotics extends BuildCraftMod {
RoboticsProxy.proxy.registerRenderers();
ListMatchHandlerClass.itemClasses.add(ItemRobot.class);
ListRegistry.itemClassAsType.add(ItemRobot.class);
}
public static void loadRecipes() {

View file

@ -51,6 +51,7 @@ import buildcraft.api.core.IIconProvider;
import buildcraft.api.facades.FacadeAPI;
import buildcraft.api.gates.GateExpansions;
import buildcraft.api.gates.IGateExpansion;
import buildcraft.api.lists.ListRegistry;
import buildcraft.api.statements.IActionInternal;
import buildcraft.api.statements.ITriggerInternal;
import buildcraft.api.statements.StatementManager;
@ -66,7 +67,6 @@ import buildcraft.core.config.ConfigManager;
import buildcraft.core.lib.items.ItemBuildCraft;
import buildcraft.core.lib.network.ChannelHandler;
import buildcraft.core.lib.utils.ColorUtils;
import buildcraft.core.list.ListMatchHandlerClass;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.transport.BlockFilteredBuffer;
import buildcraft.transport.BlockGenericPipe;
@ -552,10 +552,10 @@ public class BuildCraftTransport extends BuildCraftMod {
}
}
ListMatchHandlerClass.itemClasses.add(ItemPipe.class);
ListMatchHandlerClass.itemClasses.add(ItemGate.class);
ListMatchHandlerClass.itemClasses.add(ItemFacade.class);
ListMatchHandlerClass.itemClasses.add(ItemPipeWire.class);
ListRegistry.itemClassAsType.add(ItemPipe.class);
ListRegistry.itemClassAsType.add(ItemGate.class);
ListRegistry.itemClassAsType.add(ItemFacade.class);
ListRegistry.itemClassAsType.add(ItemPipeWire.class);
}
public void reloadConfig(ConfigManager.RestartRequirement restartType) {

View file

@ -97,6 +97,7 @@ public abstract class GuiAdvancedInterface extends GuiBuildCraft {
if (slot != null) {
slot.drawTooltip(this, mouseX, mouseY);
RenderHelper.enableGUIStandardItemLighting();
}
}

View file

@ -19,6 +19,7 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import buildcraft.BuildCraftCore;
import buildcraft.api.lists.ListMatchHandler;
import buildcraft.core.ItemList;
import buildcraft.core.lib.gui.AdvancedSlot;
import buildcraft.core.lib.gui.GuiAdvancedInterface;
@ -175,9 +176,9 @@ public class GuiListNew extends GuiAdvancedInterface implements IButtonClickEven
drawTooltipForSlotAt(par1, par2);
}
private boolean isCarryingList() {
private boolean isCarryingNonEmptyList() {
ItemStack stack = mc.thePlayer.inventory.getItemStack();
return stack != null && stack.getItem() instanceof ItemList;
return stack != null && stack.getItem() instanceof ItemList && stack.getTagCompound() != null;
}
private boolean hasListEquipped() {
@ -188,7 +189,7 @@ public class GuiListNew extends GuiAdvancedInterface implements IButtonClickEven
protected void mouseClicked(int x, int y, int b) {
super.mouseClicked(x, y, b);
if (isCarryingList() || !hasListEquipped()) {
if (isCarryingNonEmptyList() || !hasListEquipped()) {
return;
}

View file

@ -4,10 +4,14 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import buildcraft.api.lists.ListMatchHandler;
import buildcraft.api.lists.ListRegistry;
import buildcraft.core.lib.inventory.StackHelper;
import buildcraft.core.lib.utils.NBTUtils;
@ -130,13 +134,36 @@ public final class ListHandlerNew {
List<ItemStack> stackList = new ArrayList<ItemStack>();
if (stacks[0] != null) {
List<ListMatchHandler> handlers = ListRegistry.getHandlers();
List<ListMatchHandler> handlersCustom = new ArrayList<ListMatchHandler>();
ListMatchHandler.Type type = getSortingType();
for (ListMatchHandler h : handlers) {
List<ItemStack> examples = h.getClientExamples(type, stacks[0]);
if (examples != null) {
stackList.addAll(examples);
if (h.isValidSource(type, stacks[0])) {
List<ItemStack> examples = h.getClientExamples(type, stacks[0]);
if (examples != null) {
stackList.addAll(examples);
} else {
handlersCustom.add(h);
}
}
}
if (handlersCustom.size() > 0) {
for (Object o: Item.itemRegistry) {
if (o != null && o instanceof Item) {
Item i = (Item) o;
List<ItemStack> examples = new ArrayList<ItemStack>();
i.getSubItems(i, CreativeTabs.tabMisc, examples);
for (ItemStack s : examples) {
for (ListMatchHandler mh : handlersCustom) {
if (mh.matches(type, stacks[0], s, false)) {
stackList.add(s);
break;
}
}
}
}
}
}
Collections.shuffle(stackList);
}
return stackList;

View file

@ -1,17 +0,0 @@
package buildcraft.core.list;
import java.util.List;
import net.minecraft.item.ItemStack;
/**
* Internal interface for now - it will become public once its shape is set
* in stone better.
*/
public abstract class ListMatchHandler {
public enum Type {
TYPE, MATERIAL, CLASS
}
public abstract boolean matches(Type type, ItemStack stack, ItemStack target, boolean precise);
public abstract List<ItemStack> getClientExamples(Type type, ItemStack stack);
}

View file

@ -1,41 +1,26 @@
package buildcraft.core.list;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
public class ListMatchHandlerClass extends ListMatchHandler {
public static final Set<Class<? extends Item>> itemClasses = new HashSet<Class<? extends Item>>();
import buildcraft.api.lists.ListMatchHandler;
import buildcraft.api.lists.ListRegistry;
public class ListMatchHandlerClass extends ListMatchHandler {
@Override
public boolean matches(Type type, ItemStack stack, ItemStack target, boolean precise) {
if (type == Type.TYPE) {
Class kl = stack.getItem().getClass();
return itemClasses.contains(kl) && kl.equals(target.getClass());
return ListRegistry.itemClassAsType.contains(kl) && kl.equals(target.getClass());
}
return false;
}
@Override
public List<ItemStack> getClientExamples(Type type, ItemStack stack) {
public boolean isValidSource(Type type, ItemStack stack) {
if (type == Type.TYPE) {
Class kl = stack.getItem().getClass();
List<ItemStack> examples = new ArrayList<ItemStack>();
if (itemClasses.contains(kl)) {
for (Object key : Item.itemRegistry.getKeys()) {
Item i = (Item) Item.itemRegistry.getObject(key);
if (i != null && kl.equals(i.getClass())) {
i.getSubItems(i, CreativeTabs.tabMisc, examples);
}
}
}
return examples;
return ListRegistry.itemClassAsType.contains(kl);
}
return null;
return false;
}
}

View file

@ -7,6 +7,7 @@ import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import buildcraft.api.lists.ListMatchHandler;
import buildcraft.core.lib.inventory.StackHelper;
import buildcraft.core.lib.utils.FluidUtils;
@ -31,6 +32,16 @@ public class ListMatchHandlerFluid extends ListMatchHandler {
return false;
}
@Override
public boolean isValidSource(Type type, ItemStack stack) {
if (type == Type.TYPE) {
return FluidContainerRegistry.isContainer(stack);
} else if (type == Type.MATERIAL) {
return FluidUtils.getFluidStackFromItemStack(stack) != null;
}
return false;
}
@Override
public List<ItemStack> getClientExamples(Type type, ItemStack stack) {
if (type == Type.MATERIAL) {

View file

@ -7,6 +7,8 @@ import java.util.Set;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
import buildcraft.api.lists.ListMatchHandler;
import buildcraft.core.lib.inventory.StackHelper;
public class ListMatchHandlerOreDictionary extends ListMatchHandler {
@ -66,6 +68,17 @@ public class ListMatchHandlerOreDictionary extends ListMatchHandler {
return false;
}
@Override
public boolean isValidSource(Type type, ItemStack stack) {
if (OreDictionary.getOreIDs(stack).length > 0) {
return true;
}
if (type == Type.TYPE && stack.getHasSubtypes()) {
return true;
}
return false;
}
private String getBestOreString(int[] oreIds) {
String s = null, st;
int suc = 0, suct;

View file

@ -0,0 +1,35 @@
package buildcraft.core.list;
import java.util.HashSet;
import java.util.Set;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import buildcraft.api.lists.ListMatchHandler;
public class ListMatchHandlerTools extends ListMatchHandler {
public static final Set<Class<? extends Item>> itemClasses = new HashSet<Class<? extends Item>>();
@Override
public boolean matches(Type type, ItemStack stack, ItemStack target, boolean precise) {
if (type == Type.TYPE) {
Set<String> toolClassesSource = stack.getItem().getToolClasses(stack);
Set<String> toolClassesTarget = target.getItem().getToolClasses(stack);
if (toolClassesSource.size() > 0 && toolClassesTarget.size() > 0) {
for (String s : toolClassesSource) {
if (!toolClassesTarget.contains(s)) {
return false;
}
}
return true;
}
}
return false;
}
@Override
public boolean isValidSource(Type type, ItemStack stack) {
return stack.getItem().getToolClasses(stack).size() > 0;
}
}