More work on the API but IIcons are not behaving as expected

This commit is contained in:
jss2a98aj committed 2021-11-04 23:36:02 -04:00
1 parent 3773cfdcbe
commit 0a2876d407
20 files changed
+340 -72

No files matched your search

@@ -3,43 +3,57 @@ package baubles.api.expanded;
import baubles.api.BaubleType;
public class BaubleExpandedSlots {
//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";
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;
public static final String ringType = "ring";
public static final String amuletType = "amulet";
public static final String beltType = "belt";
static {
tryAddSlot(amuletType);
tryAddSlot(ringType);
tryAddSlot(ringType);
tryAddSlot(beltType);
tryAddSlot(ringType);
tryAddSlot(ringType);
tryAddSlot(ringType);
tryAddSlot(ringType);
tryAddSlot(ringType);
tryAddSlot(ringType);
}
/**
* 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) {
//TODO: block after GUI init
if(newSlotsRemaining >= 1) {
slots[slotLimit - newSlotsRemaining] = type;
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) {
//TODO: block after GUI init
for(int slotToCheck = 0; slotToCheck < slotLimit; slotToCheck++) {
if(slots[slotToCheck].equals(type)) {
for(int slotToMove = slotToCheck + 1; slotToMove < slotLimit; slotToMove++) {
@@ -53,6 +67,13 @@ public class BaubleExpandedSlots {
return false;
}
/**
* 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++) {
@@ -63,18 +84,36 @@ public class BaubleExpandedSlots {
return total;
}
/**
* 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 in the specified slot or "unknown"
*/
public static String getSlotType(int slot) {
if (slot > 0 & slot < slotLimit) {
if (slot >= 0 & slot < slotLimit) {
return slots[slot];
} else {
return null;
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:
@@ -0,0 +1,71 @@
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;
}
}
}
@@ -63,19 +63,19 @@ public class GuiPlayerExpanded extends InventoryEffectRenderer {
@Override
public void drawScreen(int par1, int par2, float par3) {
super.drawScreen(par1, par2, par3);
this.xSizeFloat = (float)par1;
this.ySizeFloat = (float)par2;
xSizeFloat = (float)par1;
ySizeFloat = (float)par2;
}
@Override
protected void drawGuiContainerBackgroundLayer(float p_146976_1_, int p_146976_2_, int p_146976_3_) {
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
this.mc.getTextureManager().bindTexture(background);
this.drawTexturedModalRect(guiLeft, guiTop, 0, 0, this.xSize, this.ySize);
mc.getTextureManager().bindTexture(background);
drawTexturedModalRect(guiLeft, guiTop, 0, 0, this.xSize, this.ySize);
for (int i1 = 4; i1 < BaubleExpandedSlots.getTotalSlots() + 4; ++i1) {
Slot slot = (Slot)this.inventorySlots.inventorySlots.get(i1);
this.drawTexturedModalRect(guiLeft + slot.xDisplayPosition - 1, guiTop + slot.yDisplayPosition - 1, 200, 0, 18, 18);
drawTexturedModalRect(guiLeft + slot.xDisplayPosition - 1, guiTop + slot.yDisplayPosition - 1, 200, 0, 18, 18);
}
drawPlayerModel(guiLeft + 51, guiTop + 75, 30, (float)(guiLeft + 51) - this.xSizeFloat, (float)(guiTop + 75 - 50) - this.ySizeFloat, this.mc.thePlayer);
}
@@ -126,11 +126,12 @@ public class GuiPlayerExpanded extends InventoryEffectRenderer {
}
@Override
protected void keyTyped(char par1, int par2) {
if (par2 == Baubles.proxy.keyHandler.key.getKeyCode()) {
protected void keyTyped(char par1, int keyCode) {
if (keyCode == Baubles.proxy.keyHandler.key.getKeyCode()) {
this.mc.thePlayer.closeScreen();
} else
super.keyTyped(par1, par2);
} else {
super.keyTyped(par1, keyCode);
}
}
}
+23 -13
View File
@@ -2,11 +2,10 @@ package baubles.common;
import java.io.File;
import net.minecraftforge.common.MinecraftForge;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import baubles.api.expanded.BaubleExpandedSlots;
import baubles.common.event.EventHandlerEntity;
import baubles.common.event.EventHandlerNetwork;
import baubles.common.network.PacketHandler;
@@ -16,51 +15,62 @@ import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.item.Item;
import net.minecraftforge.common.MinecraftForge;
@Mod(modid = Baubles.MODID, name = Baubles.MODNAME, version = Baubles.VERSION, dependencies="required-after:Forge@[10.13.2,);")
@Mod(modid = Baubles.MODID, name = Baubles.MODNAME, version = Baubles.VERSION)
public class Baubles {
public static final String MODID = "Baubles";
public static final String MODNAME = "Baubles";
public static final String VERSION = "1.0.1.10";
@SidedProxy(clientSide = "baubles.client.ClientProxy", serverSide = "baubles.common.CommonProxy")
public static CommonProxy proxy;
@Instance(value=Baubles.MODID)
public static Baubles instance;
public EventHandlerEntity entityEventHandler;
public EventHandlerNetwork entityEventNetwork;
public File modDir;
public static final Logger log = LogManager.getLogger("Baubles");
public static final int GUI = 0;
private static Item itemDebugger;
@EventHandler
public void preInit(FMLPreInitializationEvent event) {
event.getModMetadata().version = Baubles.VERSION;
modDir = event.getModConfigurationDirectory();
PacketHandler.init();
entityEventHandler = new EventHandlerEntity();
entityEventNetwork = new EventHandlerNetwork();
MinecraftForge.EVENT_BUS.register(entityEventHandler);
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);
BaubleExpandedSlots.tryAddSlot(BaubleExpandedSlots.beltType);
}
@EventHandler
public void init(FMLInitializationEvent evt) {
public void init(FMLInitializationEvent event) {
NetworkRegistry.INSTANCE.registerGuiHandler(instance, proxy);
proxy.registerKeyBindings();
}
}
@@ -0,0 +1,127 @@
package baubles.common;
import java.util.List;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.IIcon;
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;
import cpw.mods.fml.relauncher.Side;
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 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)
@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 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;
}
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 onWornTick(ItemStack itemStack, EntityLivingBase player) {
}
@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;
}
}
@@ -16,6 +16,7 @@ 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;
@@ -50,6 +51,7 @@ public class ContainerPlayerExpanded extends Container {
return itemStack.getItem().isValidArmor(itemStack, k, thePlayer);
}
@Override
@SideOnly(Side.CLIENT)
public IIcon getBackgroundIconIndex() {
return ItemArmor.func_94602_b(k);
@@ -64,7 +66,15 @@ public class ContainerPlayerExpanded extends Container {
//bauble slots
for(i = 0; i < totalBaubleSlots; i++) {
addSlotToContainer(new SlotBauble(baubles, BaubleExpandedSlots.getSlotType(i), i, slotStartX + (slotOffset * (i / 4)), slotStartY + (slotOffset * (i % 4))));
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);
}
});
}
//inventory slots
@@ -112,8 +122,7 @@ public class ContainerPlayerExpanded extends Container {
if(item instanceof IBaubleExpanded) {
types = ((IBaubleExpanded)item).getBaubleTypes(itemstack);
} else {
BaubleType legacyType = ((IBauble)item).getBaubleType(itemstack);
types = new String[] {BaubleExpandedSlots.getTypeStringFromBaubleType(legacyType)};
types = new String[] {BaubleExpandedSlots.getTypeStringFromBaubleType(((IBauble)item).getBaubleType(itemstack))};
}
for(String type : types) {
if(type.equals(BaubleExpandedSlots.getSlotType(baubleSlot - 4)) && !mergeItemStack(itemstack1, baubleSlot, baubleSlot + 1, false)) {
@@ -10,6 +10,7 @@ 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 cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@@ -48,10 +49,9 @@ public class SlotBauble extends Slot {
if(item instanceof IBaubleExpanded) {
types = ((IBaubleExpanded)item).getBaubleTypes(stack);
} else {
BaubleType legacyType = ((IBauble)item).getBaubleType(stack);
types = new String[] {BaubleExpandedSlots.getTypeStringFromBaubleType(legacyType)};
types = new String[] {BaubleExpandedSlots.getTypeStringFromBaubleType(((IBauble)item).getBaubleType(stack))};
}
for(String type : types) {
if(type.equals(slotType)) {
return true;
@@ -72,9 +72,10 @@ public class SlotBauble extends Slot {
return 1;
}
/*@SideOnly(Side.CLIENT)
/*@Override
@SideOnly(Side.CLIENT)
public IIcon getBackgroundIconIndex() {
return null;
return BaubleSlotTypeBackgroundIconManager.getBackgroundIconForSlotType(slotType);
}*/
}