Fully working proof of concept for the slot type API

This commit is contained in:
jss2a98aj committed 2021-11-05 04:30:05 -04:00
1 parent 0a2876d407
commit 50a9b2a161
8 files changed
+279 -300

No files matched your search

@@ -1,129 +1,188 @@
package baubles.api.expanded;
import java.util.ArrayList;
import baubles.api.BaubleType;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.LoaderState;
public class BaubleExpandedSlots {
//The total number of slots that can be added.
public static final int slotLimit = 20;
//The total number of slots that can be added.
public static final int slotLimit = 20;
//All "default" types will have slot backgrounds registered automatically.
public static final String ringType = "ring";
public static final String amuletType = "amulet";
public static final String beltType = "belt";
public static final String headType = "head";
public static final String bodyType = "body";
public static final String charmType = "charm";
public static final String gauntletType = "gauntlet";
public static final String capeType = "cape";
//Fallback for if a slot type cannot be found
public static final String unknown = "unknown";
//All "default" types will be registered automatically.
public static final String ringType = "ring";
public static final String amuletType = "amulet";
public static final String beltType = "belt";
public static final String headType = "head";
public static final String bodyType = "body";
public static final String charmType = "charm";
public static final String gauntletType = "gauntlet";
public static final String capeType = "cape";
public static final String[] defaultTypes = {
ringType,
amuletType,
beltType,
headType,
bodyType,
charmType,
gauntletType,
capeType
};
private static String[] slots = new String[slotLimit];
private static int newSlotsRemaining = slotLimit;
private static String[] slots = new String[slotLimit];
private static ArrayList<String> registeredTypes = new ArrayList<String>();
static {
registeredTypes.add(ringType);
registeredTypes.add(amuletType);
registeredTypes.add(beltType);
registeredTypes.add(headType);
registeredTypes.add(bodyType);
registeredTypes.add(charmType);
registeredTypes.add(gauntletType);
registeredTypes.add(capeType);
}
private static int newSlotsRemaining = slotLimit;
/**
* Attempts to add a slot, but will not if there are no slots left.
*
* @param type The type of the slot being added
* @return If adding a slot was successful
*/
public static boolean tryAddSlot(String type) {
if(newSlotsRemaining >= 1) {
slots[slotLimit - newSlotsRemaining] = type;
newSlotsRemaining--;
return true;
}
//TODO: Sort slots, locked after post init
//TODO: Lock removing slots added during pre init
/**
* Tries to add a new slot type if the loader state is currently
* in Pre Initialization and the slot type has not already been registered.
*
* @return If adding a type was successful
*/
public static boolean tryRegisterType(String type) {
if(type != null && type.length() > 0 && Loader.instance().getLoaderState() == LoaderState.PREINITIALIZATION && !registeredTypes.contains(type)) {
registeredTypes.add(type);
return true;
} else {
return false;
}
}
public static int indexOfTypeInRegisteredTypes(String type) {
return registeredTypes.indexOf(type);
}
/**
* Returns if the type is registered or not.
*
* @return If the type is registered or not
*/
public static boolean isTypeRegistered(String type) {
return registeredTypes.contains(type);
}
/**
* Returns the currently registered bauble types.
* If Pre Initialization is done this list is effectively final.
*
* @return The currently registered bauble types
*/
public static ArrayList<String> getCurrentlyRegisteredTypes() {
return registeredTypes;
}
/**
* Returns if a new slot was added successfully.
* Does not add a slot type if the type is unregistered or no slots are free.
*
* @param type The type of the slot to be added
* @return If adding a slot was successful or not
*/
public static boolean tryAddSlot(String type) {
if(newSlotsRemaining >= 1 && registeredTypes.contains(type)) {
slots[slotLimit - newSlotsRemaining] = type;
newSlotsRemaining--;
return true;
} else {
return false;
}
}
return false;
}
/**
* Remove a slot of the specified type if one can be found.
*
* @param type The type of the slot to be removed
* @return If removing a slot was successful or not
*/
public static boolean tryRemoveSlot(String type) {
if(Loader.instance().getLoaderState() == LoaderState.PREINITIALIZATION && newSlotsRemaining < slotLimit)
for(int slotToCheck = 0; slotToCheck < slotsCurrentlyUsed(); slotToCheck++) {
if(slots[slotToCheck].equals(type)) {
for(int slotToMove = slotToCheck + 1; slotToMove < slotLimit; slotToMove++) {
slots[slotToMove - 1] = slots[slotToMove];
}
slots[slotLimit - 1] = null;
newSlotsRemaining++;
return true;
}
}
return false;
}
/**
* Remove a slot of the specified type if one could be found.
*
* @param type The type of the slot being removed
* @return If removing a slot was successful
*/
public static boolean tryRemoveSlot(String type) {
for(int slotToCheck = 0; slotToCheck < slotLimit; slotToCheck++) {
if(slots[slotToCheck].equals(type)) {
for(int slotToMove = slotToCheck + 1; slotToMove < slotLimit; slotToMove++) {
slots[slotToMove - 1] = slots[slotToMove];
}
slots[slotLimit - 1] = null;
newSlotsRemaining++;
return true;
}
}
return false;
}
/**
* Returns the current number of slots with a matching type.
*
* @param type The type of slot being counted
* @return The current total number of matching slots found
*/
public static int totalCurrentSlotsOfType(String type) {
int total = 0;
for(int slotToCheck = 0; slotToCheck < slotLimit; slotToCheck++) {
if(slots[slotToCheck].equals(type)) {
total++;
}
}
return total;
}
/**
* Returns the currently number of slots matching the specified type.
*
* @param type The type of slot being counted
* @return The current total number of matching slots found
*/
public static int totalCurrentSlotsOfType(String type) {
int total = 0;
for(int slotToCheck = 0; slotToCheck < slotLimit; slotToCheck++) {
if(slots[slotToCheck].equals(type)) {
total++;
}
}
return total;
}
/**
* Returns the number of bauble slots that are currently used.
*
* @return The number of bauble slots currently used
*/
public static int slotsCurrentlyUsed() {
return slotLimit - newSlotsRemaining;
}
/**
* Returns the number of bauble slots that are currently unused.
*
* @return The number of bauble slots currently unused
*/
public static int slotsCurrentlyUnused() {
return newSlotsRemaining;
}
/**
* Returns the number of slots that are currently used.
*
* @return The total number of slots currently used
*/
public static int getTotalSlots() {
return slotLimit - newSlotsRemaining;
}
/**
* Returns the type of the specified slot or null if none or out of range.
*
* @param slot The slot to check
* @return The type of the specified slot or "unknown"
*/
public static String getSlotType(int slot) {
if (slot >= 0 & slot < slotLimit) {
return slots[slot];
} else {
return unknown;
}
}
/**
* Returns the type of the specified slot or null if none or out of range.
*
* @param slot The slot to check
* @return The type in the specified slot or "unknown"
*/
public static String getSlotType(int slot) {
if (slot >= 0 & slot < slotLimit) {
return slots[slot];
} else {
return "unknown";
}
}
/**
* Returns the type based on the specified BaubleType.
*
* @param type The BaubleType get a matching type from
* @return The type matching the BaubleType or "unknown"
*/
public static String getTypeStringFromBaubleType(BaubleType type) {
switch(type) {
case RING:
return ringType;
case AMULET:
return amuletType;
case BELT:
return beltType;
}
return "unknown";
}
/**
* Returns the type based on the specified BaubleType.
*
* @param type The BaubleType to get a matching type from
* @return The type matching the BaubleType or "unknown"
*/
public static String getTypeFromBaubleType(BaubleType type) {
switch(type) {
case RING:
return ringType;
case AMULET:
return amuletType;
case BELT:
return beltType;
default:
return unknown;
}
}
}
@@ -1,71 +0,0 @@
package baubles.api.expanded;
import java.util.ArrayList;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.LoaderState;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.util.IIcon;
public class BaubleSlotTypeBackgroundIconManager {
@SideOnly(Side.CLIENT)
private static class TypeIIconPair {
public IIcon icon;
public String type;
public TypeIIconPair(IIcon icon, String type){
this.icon = icon;
this.type = type;
}
}
@SideOnly(Side.CLIENT)
private static ArrayList<TypeIIconPair> typeIIconPairList = new ArrayList<TypeIIconPair>();
@SideOnly(Side.CLIENT)
private static IIcon unknownIcon;
@SideOnly(Side.CLIENT)
public static IIcon getBackgroundIconForSlotType(String type) {
if(type != null) {
for(TypeIIconPair pair : typeIIconPairList) {
if(type.equals(pair.type)) {
return pair.icon;
}
}
}
return unknownIcon;
}
@SideOnly(Side.CLIENT)
public static boolean doesBackgroundIconForSlotTypeExist(String type) {
if(type != null) {
for(TypeIIconPair pair : typeIIconPairList) {
if(type.equals(pair.type)) {
return true;
}
}
}
return false;
}
@SideOnly(Side.CLIENT)
public static boolean tryAddBackgroundIconForSlotType(IIcon icon, String type) {
if(icon != null && type != null && !doesBackgroundIconForSlotTypeExist(type)) {
typeIIconPairList.add(new TypeIIconPair(icon, type));
return true;
}
return false;
}
@SideOnly(Side.CLIENT)
public static void setBakgroundIconForUnknownSlotType(IIcon icon) {
if(icon != null) {
unknownIcon = icon;
}
}
}
@@ -73,7 +73,7 @@ public class GuiPlayerExpanded extends InventoryEffectRenderer {
mc.getTextureManager().bindTexture(background);
drawTexturedModalRect(guiLeft, guiTop, 0, 0, this.xSize, this.ySize);
for (int i1 = 4; i1 < BaubleExpandedSlots.getTotalSlots() + 4; ++i1) {
for (int i1 = 4; i1 < BaubleExpandedSlots.slotsCurrentlyUsed() + 4; ++i1) {
Slot slot = (Slot)this.inventorySlots.inventorySlots.get(i1);
drawTexturedModalRect(guiLeft + slot.xDisplayPosition - 1, guiTop + slot.yDisplayPosition - 1, 200, 0, 18, 18);
}
+2 -4
View File
@@ -42,7 +42,7 @@ public class Baubles {
public static final Logger log = LogManager.getLogger("Baubles");
public static final int GUI = 0;
private static Item itemDebugger;
public static final Item itemDebugger = new ItemDebugger().setUnlocalizedName("baubleSlotDebugTool");
@EventHandler
public void preInit(FMLPreInitializationEvent event) {
@@ -58,9 +58,6 @@ public class Baubles {
FMLCommonHandler.instance().bus().register(entityEventNetwork);
proxy.registerHandlers();
itemDebugger = new ItemDebugger().setUnlocalizedName("baubleSlotDebugTool");
GameRegistry.registerItem(itemDebugger, "bauble_slot_debug_tool", Baubles.MODID);
BaubleExpandedSlots.tryAddSlot(BaubleExpandedSlots.amuletType);
BaubleExpandedSlots.tryAddSlot(BaubleExpandedSlots.ringType);
BaubleExpandedSlots.tryAddSlot(BaubleExpandedSlots.ringType);
@@ -71,6 +68,7 @@ public class Baubles {
public void init(FMLInitializationEvent event) {
NetworkRegistry.INSTANCE.registerGuiHandler(instance, proxy);
proxy.registerKeyBindings();
GameRegistry.registerItem(itemDebugger, "bauble_slot_debug_tool", Baubles.MODID);
}
}
+91 -90
View File
@@ -16,7 +16,6 @@ import net.minecraft.world.World;
import baubles.api.BaubleType;
import baubles.api.IBauble;
import baubles.api.expanded.BaubleExpandedSlots;
import baubles.api.expanded.BaubleSlotTypeBackgroundIconManager;
import baubles.api.expanded.IBaubleExpanded;
import baubles.common.container.InventoryBaubles;
import baubles.common.lib.PlayerHandler;
@@ -25,103 +24,105 @@ import cpw.mods.fml.relauncher.SideOnly;
public class ItemDebugger extends Item implements IBaubleExpanded {
public ItemDebugger() {
super();
this.setMaxStackSize(1);
this.setHasSubtypes(true);
setCreativeTab(CreativeTabs.tabTools);
}
public ItemDebugger() {
super();
this.setMaxStackSize(1);
this.setHasSubtypes(true);
setCreativeTab(CreativeTabs.tabTools);
}
public IIcon[] icons;
@SideOnly(Side.CLIENT)
@Override
public void registerIcons(IIconRegister ir) {
icons = new IIcon[1 + BaubleExpandedSlots.defaultTypes.length];
icons[0] = ir.registerIcon("baubles:empty_bauble_slot_unknown");
BaubleSlotTypeBackgroundIconManager.setBakgroundIconForUnknownSlotType(icons[0]);
for(int i = 1; i < icons.length; i++) {
String type = BaubleExpandedSlots.defaultTypes[i - 1];
icons[i] = ir.registerIcon("baubles:empty_bauble_slot_" + type);
BaubleSlotTypeBackgroundIconManager.tryAddBackgroundIconForSlotType(icons[i], type);
}
}
@SideOnly(Side.CLIENT)
@Override
public IIcon getIconFromDamage(int meta) {
return icons[meta >= icons.length ? 0 : meta];
}
@SideOnly(Side.CLIENT)
private IIcon[] icons;
@SideOnly(Side.CLIENT)
@Override
public void registerIcons(IIconRegister ir) {
icons = new IIcon[1 + BaubleExpandedSlots.getCurrentlyRegisteredTypes().size()];
icons[0] = ir.registerIcon("baubles:empty_bauble_slot_unknown");
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(Item item, CreativeTabs tab, List list) {
for(int i = 0; i < icons.length; i++) {
list.add(new ItemStack(this, 1, i));
}
}
for(int i = 1; i < icons.length; i++) {
icons[i] = ir.registerIcon("baubles:empty_bauble_slot_" + BaubleExpandedSlots.getCurrentlyRegisteredTypes().get(i - 1));
}
}
/*@Override
public EnumRarity getRarity(ItemStack itemStack) {
return EnumRarity.rare;
}*/
@Override
public String[] getBaubleTypes(ItemStack itemStack) {
String type;
int meta = itemStack.getItemDamage();
if(meta <= 0 || meta > BaubleExpandedSlots.defaultTypes.length) {
type = "unknown";
} else {
type = BaubleExpandedSlots.defaultTypes[meta - 1];
}
return new String[] {type};
}
@Override
public BaubleType getBaubleType(ItemStack itemStack) {
return null;
}
@SideOnly(Side.CLIENT)
@Override
public IIcon getIconFromDamage(int meta) {
return icons[meta >= icons.length ? 0 : meta];
}
public void addInformation(ItemStack itemStack, EntityPlayer player, List list, boolean par4) {
list.add("Valid bauble slot types:");
for(String type : getBaubleTypes(itemStack)) {
list.add(type);
}
}
@SideOnly(Side.CLIENT)
@Override
public void getSubItems(Item item, CreativeTabs tab, List list) {
for(int i = 0; i < icons.length; i++) {
list.add(new ItemStack(this, 1, i));
}
}
@Override
public void onWornTick(ItemStack itemStack, EntityLivingBase player) {
}
@Override
public String[] getBaubleTypes(ItemStack itemStack) {
String type;
int meta = itemStack.getItemDamage();
if(meta <= 0 || meta >= icons.length) {
type = BaubleExpandedSlots.unknown;
} else {
type = BaubleExpandedSlots.getCurrentlyRegisteredTypes().get(meta - 1);
}
return new String[] {type};
}
@Override
public boolean hasEffect(ItemStack itemStack, int a) {
return true;
}
@Override
public BaubleType getBaubleType(ItemStack itemStack) {
return null;
}
@Override
public String getUnlocalizedName(ItemStack itemStack) {
return super.getUnlocalizedName();// + "." + itemStack.getItemDamage();
}
public void addInformation(ItemStack itemStack, EntityPlayer player, List list, boolean par4) {
list.add("Valid bauble slot types:");
for(String type : getBaubleTypes(itemStack)) {
list.add(type);
}
}
@Override
public void onEquipped(ItemStack itemStack, EntityLivingBase player) {
}
@Override
public void onWornTick(ItemStack itemStack, EntityLivingBase player) {
}
@Override
public void onUnequipped(ItemStack itemStack, EntityLivingBase player) {
}
@Override
public boolean canEquip(ItemStack itemStack, EntityLivingBase player) {
return true;
}
@Override
public boolean canUnequip(ItemStack itemStack, EntityLivingBase player) {
return true;
}
@Override
public boolean hasEffect(ItemStack itemStack, int a) {
return true;
}
@Override
public String getUnlocalizedName(ItemStack itemStack) {
return super.getUnlocalizedName();// + "." + itemStack.getItemDamage();
}
@Override
public void onEquipped(ItemStack itemStack, EntityLivingBase player) {
}
@Override
public void onUnequipped(ItemStack itemStack, EntityLivingBase player) {
}
@Override
public boolean canEquip(ItemStack itemStack, EntityLivingBase player) {
return true;
}
@Override
public boolean canUnequip(ItemStack itemStack, EntityLivingBase player) {
return true;
}
@SideOnly(Side.CLIENT)
public IIcon getBackgroundIconForSlotType(String type) {
if(type != null && BaubleExpandedSlots.isTypeRegistered(type)) {
return icons[1 + BaubleExpandedSlots.indexOfTypeInRegisteredTypes(type)];
} else {
return icons[0];
}
}
}
@@ -16,7 +16,6 @@ import net.minecraft.util.IIcon;
import baubles.api.BaubleType;
import baubles.api.IBauble;
import baubles.api.expanded.BaubleExpandedSlots;
import baubles.api.expanded.BaubleSlotTypeBackgroundIconManager;
import baubles.api.expanded.IBaubleExpanded;
import baubles.common.lib.PlayerHandler;
import cpw.mods.fml.relauncher.Side;
@@ -62,19 +61,11 @@ public class ContainerPlayerExpanded extends Container {
final int slotOffset = 18;
final int slotStartX = 80;
final int slotStartY = 8;
final int totalBaubleSlots = BaubleExpandedSlots.getTotalSlots();
final int totalBaubleSlots = BaubleExpandedSlots.slotsCurrentlyUsed();
//bauble slots
for(i = 0; i < totalBaubleSlots; i++) {
final String type = BaubleExpandedSlots.getSlotType(i);
addSlotToContainer(new SlotBauble(baubles, BaubleExpandedSlots.getSlotType(i), i, slotStartX + (slotOffset * (i / 4)), slotStartY + (slotOffset * (i % 4))) {
@Override
@SideOnly(Side.CLIENT)
public IIcon getBackgroundIconIndex() {
//Not working?
return BaubleSlotTypeBackgroundIconManager.getBackgroundIconForSlotType(type);
}
});
addSlotToContainer(new SlotBauble(baubles, BaubleExpandedSlots.getSlotType(i), i, slotStartX + (slotOffset * (i / 4)), slotStartY + (slotOffset * (i % 4))));
}
//inventory slots
@@ -103,7 +94,7 @@ public class ContainerPlayerExpanded extends Container {
public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex) {
ItemStack itemstack = null;
Slot slot = (Slot)inventorySlots.get(slotIndex);
final int totalBaubleSlots = BaubleExpandedSlots.getTotalSlots();
final int totalBaubleSlots = BaubleExpandedSlots.slotsCurrentlyUsed();
if (slot != null && slot.getHasStack()) {
ItemStack itemstack1 = slot.getStack();
@@ -122,7 +113,7 @@ public class ContainerPlayerExpanded extends Container {
if(item instanceof IBaubleExpanded) {
types = ((IBaubleExpanded)item).getBaubleTypes(itemstack);
} else {
types = new String[] {BaubleExpandedSlots.getTypeStringFromBaubleType(((IBauble)item).getBaubleType(itemstack))};
types = new String[] {BaubleExpandedSlots.getTypeFromBaubleType(((IBauble)item).getBaubleType(itemstack))};
}
for(String type : types) {
if(type.equals(BaubleExpandedSlots.getSlotType(baubleSlot - 4)) && !mergeItemStack(itemstack1, baubleSlot, baubleSlot + 1, false)) {
@@ -210,7 +210,7 @@ public class InventoryBaubles implements IInventory {
types = ((IBaubleExpanded)item).getBaubleTypes(stack);
} else {
BaubleType legacyType = ((IBauble)item).getBaubleType(stack);
types = new String[] {BaubleExpandedSlots.getTypeStringFromBaubleType(legacyType)};
types = new String[] {BaubleExpandedSlots.getTypeFromBaubleType(legacyType)};
}
for(String type : types) {
@@ -10,8 +10,9 @@ import net.minecraft.util.IIcon;
import baubles.api.BaubleType;
import baubles.api.IBauble;
import baubles.api.expanded.BaubleExpandedSlots;
import baubles.api.expanded.BaubleSlotTypeBackgroundIconManager;
import baubles.api.expanded.IBaubleExpanded;
import baubles.common.Baubles;
import baubles.common.ItemDebugger;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@@ -22,7 +23,7 @@ public class SlotBauble extends Slot {
@Deprecated
public SlotBauble(IInventory inventory, BaubleType legacyType, int slot, int x, int y) {
super(inventory, slot, x, y);
slotType = BaubleExpandedSlots.getTypeStringFromBaubleType(legacyType);
slotType = BaubleExpandedSlots.getTypeFromBaubleType(legacyType);
}
@@ -49,7 +50,7 @@ public class SlotBauble extends Slot {
if(item instanceof IBaubleExpanded) {
types = ((IBaubleExpanded)item).getBaubleTypes(stack);
} else {
types = new String[] {BaubleExpandedSlots.getTypeStringFromBaubleType(((IBauble)item).getBaubleType(stack))};
types = new String[] {BaubleExpandedSlots.getTypeFromBaubleType(((IBauble)item).getBaubleType(stack))};
}
for(String type : types) {
@@ -72,10 +73,10 @@ public class SlotBauble extends Slot {
return 1;
}
/*@Override
@Override
@SideOnly(Side.CLIENT)
public IIcon getBackgroundIconIndex() {
return BaubleSlotTypeBackgroundIconManager.getBackgroundIconForSlotType(slotType);
}*/
return ((ItemDebugger) Baubles.itemDebugger).getBackgroundIconForSlotType(slotType);
}
}