Code refactoring

This commit is contained in:
Alexander Anishin committed 2023-04-29 13:11:51 +03:00
1 parent e24a94f0ba
commit f3d3a0a2df
11 files changed
+270 -546

No files matched your search

@@ -12,15 +12,27 @@ import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class ContainerEffectJar extends effectContainer {
public class ContainerEffectJar extends Container {
public TileEntityEffectJar jar;
public int lastBeeHealth = 0;
public int speciesColour = 0xffffff;
TileEntityEffectJar jar;
public ContainerEffectJar(TileEntityEffectJar tileEntityEffectJar, EntityPlayer player) {
super(tileEntityEffectJar, player);
jar = (TileEntityEffectJar) inventory;
this.jar = tileEntityEffectJar;
this.addSlotToContainer(
new SlotCustomItems(this.jar, 0, 80, 22, GameRegistry.findItemStack("Forestry", "beeDroneGE", 1)));
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 9; ++j) {
this.addSlotToContainer(new Slot(player.inventory, j + i * 9 + 9, 8 + j * 18, 74 + i * 18));
}
}
for (int i = 0; i < 9; ++i) {
this.addSlotToContainer(new Slot(player.inventory, i, 8 + i * 18, 132));
}
}
@Override
@@ -63,4 +75,29 @@ public class ContainerEffectJar extends effectContainer {
public boolean canInteractWith(EntityPlayer entityplayer) {
return this.jar.isUseableByPlayer(entityplayer);
}
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int slot) {
Slot itemSlot = this.getSlot(slot);
boolean clearSlot = false;
if (itemSlot != null && itemSlot.getHasStack()) {
ItemStack srcStack = itemSlot.getStack();
if (slot == 0 && srcStack != null) {
clearSlot = this.mergeItemStack(srcStack, 1, 36 + 1, false);
} else {
if (this.getSlot(0).isItemValid(srcStack)) {
clearSlot = this.mergeItemStack(srcStack, 0, 1, false);
}
}
}
if (clearSlot) {
itemSlot.putStack(null);
}
itemSlot.onSlotChanged();
player.inventory.markDirty();
return null;
}
}
@@ -2,26 +2,16 @@ package magicbees.client.gui;
import magicbees.itemInventories.InventoryBeeRing;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.entity.player.InventoryPlayer;
import cpw.mods.fml.common.registry.GameRegistry;
import forestry.core.gui.ContainerItemInventory;
public class ContainerEffectRing extends effectContainer {
public class ContainerEffectRing extends ContainerItemInventory<InventoryBeeRing> {
public ContainerEffectRing(InventoryBeeRing IBR, EntityPlayer player) {
super(IBR, player);
}
@Override
public boolean canInteractWith(EntityPlayer player) {
return true;
}
@Override
public void detectAndSendChanges() {
super.detectAndSendChanges();
public ContainerEffectRing(InventoryBeeRing inventory, InventoryPlayer playerInventory) {
super(inventory, playerInventory, 8, 74);
addSlotToContainer(
new SlotCustomItems(inventory, 0, 80, 22, GameRegistry.findItemStack("Forestry", "beeDroneGE", 1)));
}
}
@@ -9,14 +9,24 @@ import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
public class GUIEffectJar extends effectGui {
public class GUIEffectJar extends GuiContainer {
public static final String BACKGROUND_FILE = "jarScreen.png";
public static final ResourceLocation BACKGROUND_LOCATION = new ResourceLocation(
CommonProxy.DOMAIN,
CommonProxy.GUI_TEXTURE + BACKGROUND_FILE);
CommonProxy.GUI_TEXTURE + "jarScreen.png");
private static final int WIDTH = 176;
private static final int HEIGHT = 156;
private static final int BAR_DEST_X = 117;
private static final int BAR_DEST_Y = 10;
private static final int BAR_SRC_X = 176;
private static final int BAR_SRC_Y = 0;
private static final int BAR_WIDTH = 10;
private static final int BAR_HEIGHT = 40;
public GUIEffectJar(TileEntityEffectJar jar, EntityPlayer player) {
super(new ContainerEffectJar(jar, player));
@@ -24,6 +34,11 @@ public class GUIEffectJar extends effectGui {
this.ySize = HEIGHT;
}
@Override
protected void drawGuiContainerForegroundLayer(int par1, int par2) {
fontRendererObj.drawString("Inventory", 9, 63, 0);
}
@Override
protected void drawGuiContainerBackgroundLayer(float f, int i, int j) {
GL11.glColor4f(1f, 1f, 1f, 1f);
@@ -32,7 +47,7 @@ public class GUIEffectJar extends effectGui {
this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, xSize, ySize);
TileEntityEffectJar jar = (TileEntityEffectJar) ((ContainerEffectJar) this.inventorySlots).inventory;
TileEntityEffectJar jar = ((ContainerEffectJar) this.inventorySlots).jar;
float r = ((jar.currentBeeColour >> 16) & 255) / 255f;
float g = ((jar.currentBeeColour >> 8) & 255) / 255f;
float b = (jar.currentBeeColour & 255) / 255f;
@@ -1,50 +1,57 @@
package magicbees.client.gui;
import magicbees.item.ItemBeeRing;
import magicbees.itemInventories.InventoryBeeRing;
import magicbees.main.CommonProxy;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
public class GUIEffectRing extends effectGui {
import forestry.core.gui.GuiForestry;
public class GUIEffectRing extends GuiForestry<ContainerEffectRing, IInventory> {
public static final String BACKGROUND_FILE = "ringScreen.png";
public static final ResourceLocation BACKGROUND_LOCATION = new ResourceLocation(
CommonProxy.DOMAIN,
CommonProxy.GUI_TEXTURE + BACKGROUND_FILE);
private static final int BAR_DEST_X = 117;
private static final int BAR_DEST_Y = 10;
public GUIEffectRing(ItemStack itemStack, EntityPlayer player) {
super(
new ContainerEffectRing(
ItemBeeRing.getInventory(player, itemStack, player.inventory.currentItem, false),
player));
this.xSize = WIDTH;
this.ySize = HEIGHT;
private static final int BAR_SRC_X = 176;
private static final int BAR_SRC_Y = 0;
private static final int BAR_WIDTH = 10;
private static final int BAR_HEIGHT = 40;
private final InventoryBeeRing inventoryBeeRing;
protected GUIEffectRing(InventoryBeeRing inventory, InventoryPlayer playerInventory) {
super(BACKGROUND_LOCATION, new ContainerEffectRing(inventory, playerInventory), inventory);
this.inventoryBeeRing = inventory;
}
@Override
protected void drawGuiContainerBackgroundLayer(float f, int i, int j) {
protected void drawGuiContainerBackgroundLayer(float f, int mouseX, int mouseY) {
super.drawGuiContainerBackgroundLayer(f, mouseX, mouseY);
GL11.glColor4f(1f, 1f, 1f, 1f);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_SRC_ALPHA);
this.mc.getTextureManager().bindTexture(BACKGROUND_LOCATION);
this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, xSize, ySize);
InventoryBeeRing IBR = (InventoryBeeRing) ((ContainerEffectRing) this.inventorySlots).inventory;
float r = ((IBR.currentBeeColour >> 16) & 255) / 255f;
float g = ((IBR.currentBeeColour >> 8) & 255) / 255f;
float b = (IBR.currentBeeColour & 255) / 255f;
int currentBeeColour = this.inventoryBeeRing.getCurrentBeeColour();
float r = ((currentBeeColour >> 16) & 255) / 255f;
float g = ((currentBeeColour >> 8) & 255) / 255f;
float b = (currentBeeColour & 255) / 255f;
GL11.glColor3f(r, g, b);
int value = BAR_HEIGHT - (IBR.currentBeeHealth * BAR_HEIGHT) / 100;
int value = BAR_HEIGHT - (this.inventoryBeeRing.getCurrentBeeHealth() * BAR_HEIGHT) / 100;
this.drawTexturedModalRect(
this.guiLeft + BAR_DEST_X,
this.guiTop + value + BAR_DEST_Y,
@@ -1,6 +1,6 @@
package magicbees.client.gui;
import magicbees.item.ItemBeeRing;
import magicbees.itemInventories.InventoryBeeRing;
import magicbees.tileentity.TileEntityEffectJar;
import magicbees.tileentity.TileEntityMagicApiary;
@@ -21,9 +21,7 @@ public class GUIHandler implements IGuiHandler {
TileEntityMagicApiary tileEntityThaumicApiary = (TileEntityMagicApiary) world.getTileEntity(x, y, z);
value = new ContainerMagicApiary(player.inventory, tileEntityThaumicApiary);
} else if (ID == UIScreens.EFFECT_RING.ordinal()) {
value = new ContainerEffectRing(
ItemBeeRing.getInventory(player, player.getHeldItem(), player.inventory.currentItem, false),
player);
value = new ContainerEffectRing(new InventoryBeeRing(player.getHeldItem(), player), player.inventory);
}
return value;
@@ -37,7 +35,7 @@ public class GUIHandler implements IGuiHandler {
TileEntityMagicApiary tileEntityThaumicApiary = (TileEntityMagicApiary) world.getTileEntity(x, y, z);
return new GuiMagicApiary(player.inventory, tileEntityThaumicApiary);
} else if (ID == UIScreens.EFFECT_RING.ordinal()) {
return new GUIEffectRing(player.getHeldItem(), player);
return new GUIEffectRing(new InventoryBeeRing(player.getHeldItem(), player), player.inventory);
}
return null;
}
@@ -1,55 +0,0 @@
package magicbees.client.gui;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
public abstract class effectContainer extends Container {
public IInventory inventory;
public effectContainer(IInventory inventory, EntityPlayer player) {
this.inventory = inventory;
this.addSlotToContainer(
new SlotCustomItems(this.inventory, 0, 80, 22, GameRegistry.findItemStack("Forestry", "beeDroneGE", 1)));
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 9; ++j) {
this.addSlotToContainer(new Slot(player.inventory, j + i * 9 + 9, 8 + j * 18, 74 + i * 18));
}
}
for (int i = 0; i < 9; ++i) {
this.addSlotToContainer(new Slot(player.inventory, i, 8 + i * 18, 132));
}
}
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int slot) {
Slot itemSlot = this.getSlot(slot);
boolean clearSlot = false;
if (itemSlot != null && itemSlot.getHasStack()) {
ItemStack srcStack = itemSlot.getStack();
if (slot == 0 && srcStack != null) {
clearSlot = this.mergeItemStack(srcStack, 1, 36 + 1, false);
} else {
if (this.getSlot(0).isItemValid(srcStack)) {
clearSlot = this.mergeItemStack(srcStack, 0, 1, false);
}
}
}
if (clearSlot) {
itemSlot.putStack(null);
}
itemSlot.onSlotChanged();
player.inventory.markDirty();
return null;
}
}
@@ -1,31 +0,0 @@
package magicbees.client.gui;
import magicbees.itemInventories.InventoryBeeRing;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.inventory.Container;
import org.lwjgl.opengl.GL11;
public abstract class effectGui extends GuiContainer {
protected static final int WIDTH = 176;
protected static final int HEIGHT = 156;
protected static final int BAR_DEST_X = 117;
protected static final int BAR_DEST_Y = 10;
protected static final int BAR_SRC_X = 176;
protected static final int BAR_SRC_Y = 0;
protected static final int BAR_WIDTH = 10;
protected static final int BAR_HEIGHT = 40;
public effectGui(Container cont) {
super(cont);
}
@Override
protected void drawGuiContainerForegroundLayer(int par1, int par2) {
fontRendererObj.drawString("Inventory", 9, 63, 0);
}
}
+93 -110
View File
@@ -1,52 +1,63 @@
package magicbees.item;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import magicbees.bees.BeeManager;
import magicbees.client.gui.UIScreens;
import magicbees.itemInventories.InventoryBeeRing;
import magicbees.main.CommonProxy;
import magicbees.main.MagicBees;
import magicbees.main.utils.TabMagicBees;
import magicbees.tileentity.RingHousing;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import thaumcraft.common.Thaumcraft;
import baubles.api.BaubleType;
import baubles.api.IBauble;
import baubles.common.container.InventoryBaubles;
import baubles.common.lib.PlayerHandler;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import forestry.api.apiculture.IBee;
import forestry.api.genetics.IEffectData;
public class ItemBeeRing extends Item implements IBauble {
@SideOnly(Side.CLIENT)
private IIcon icon;
private final int DRONE_SLOT = 0;
private final int QUEEN_SLOT = 1;
private static final Map<UUID, IEffectData[][]> PLAYER_EFFECTS = new HashMap<>();
public ItemBeeRing() {
super();
this.maxStackSize = 1;
this.canRepair = false;
this.setMaxStackSize(1);
this.setNoRepair();
this.setMaxDamage(0);
this.setCreativeTab(Thaumcraft.tabTC);
this.setTextureName(CommonProxy.DOMAIN + ":beeRing");
this.setCreativeTab(TabMagicBees.tabMagicBees);
this.setUnlocalizedName("beeRing");
GameRegistry.registerItem(this, "beeRing");
}
// Equips item if shift is held and otherwise it opens the GUI
@Override
public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) {
if (!world.isRemote) {
if (!player.isSneaking()) {
if (player.isSneaking()) {
InventoryBaubles baubles = PlayerHandler.getPlayerBaubles(player);
for (int i = 0; i < baubles.getSizeInventory(); i++) {
if (baubles.isItemValidForSlot(i, itemStack)) {
ItemStack stackInSlot = baubles.getStackInSlot(i);
if (stackInSlot == null) {
baubles.setInventorySlotContents(i, itemStack.copy());
if (!player.capabilities.isCreativeMode)
player.inventory.setInventorySlotContents(player.inventory.currentItem, null);
break;
}
}
}
} else {
player.openGui(
MagicBees.object,
UIScreens.EFFECT_RING.ordinal(),
@@ -54,28 +65,11 @@ public class ItemBeeRing extends Item implements IBauble {
(int) player.posX,
(int) player.posY,
(int) player.posZ);
} else {
InventoryBaubles baubles = PlayerHandler.getPlayerBaubles(player);
for (int i = 0; i < baubles.getSizeInventory(); ++i) {
if (baubles.getStackInSlot(i) == null && baubles.isItemValidForSlot(i, itemStack)) {
baubles.setInventorySlotContents(i, itemStack.copy());
player.inventory.setInventorySlotContents(player.inventory.currentItem, null);
this.onEquipped(itemStack, player);
break;
}
}
}
}
return itemStack;
}
@Override
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister iconRegister) {
this.itemIcon = iconRegister.registerIcon(CommonProxy.DOMAIN + ":beeRing");
}
@Override
public BaubleType getBaubleType(ItemStack itemStack) {
return BaubleType.RING;
@@ -83,109 +77,103 @@ public class ItemBeeRing extends Item implements IBauble {
@Override
public void onWornTick(ItemStack itemStack, EntityLivingBase entityLivingBase) {
if (!(entityLivingBase instanceof EntityPlayer)) {
return;
}
// If I'm being honest I'm not sure if this needs to go here, but it works if I put it here.
if (!entityLivingBase.worldObj.isRemote) {
if (entityLivingBase instanceof EntityPlayer) {
InventoryBaubles baubles = PlayerHandler.getPlayerBaubles((EntityPlayer) entityLivingBase);
int slot = -1;
for (int i = 0; i < baubles.getSizeInventory(); i++) {
if (baubles.getStackInSlot(i) == itemStack) {
slot = i;
break;
}
}
EntityPlayer player = (EntityPlayer) entityLivingBase;
InventoryBeeRing inventoryBeeRing = new InventoryBeeRing(itemStack, player);
// Logic to run the ring (this logic could be moved out of the item class, but it seems fine here)
if (slot != -1) {
InventoryBeeRing inventoryRing = getInventory((EntityPlayer) entityLivingBase, itemStack, slot, true);
if (hasQueen(itemStack, (EntityPlayer) entityLivingBase, slot, inventoryRing)) {
tickQueen(itemStack, (EntityPlayer) entityLivingBase, slot, inventoryRing);
} else if (hasDrone(itemStack, (EntityPlayer) entityLivingBase, slot, inventoryRing)) {
createQueenFromDrone(itemStack, (EntityPlayer) entityLivingBase, slot, inventoryRing);
}
}
}
if (inventoryBeeRing.hasQueen()) {
tickQueen(player, inventoryBeeRing);
} else if (inventoryBeeRing.hasDrone()) {
createQueenFromDrone(inventoryBeeRing);
}
}
private void createQueenFromDrone(ItemStack itemStack, EntityPlayer player, int slot, InventoryBeeRing ringInventory) {
ItemStack droneStack = ringInventory.getStackInSlot(0);
if (magicbees.bees.BeeManager.beeRoot.isDrone(droneStack)) {
IBee bee = magicbees.bees.BeeManager.beeRoot.getMember(droneStack);
private void createQueenFromDrone(InventoryBeeRing inventoryBeeRing) {
ItemStack droneStack = inventoryBeeRing.getDrone();
if (BeeManager.beeRoot.isDrone(droneStack)) {
IBee bee = BeeManager.beeRoot.getMember(droneStack);
if (droneStack.stackSize == 1) {
ringInventory.setInventorySlotContents(0, null);
inventoryBeeRing.setDrone(null);
} else {
droneStack.stackSize = droneStack.stackSize - 1;
ringInventory.setInventorySlotContents(0, droneStack);
droneStack.stackSize--;
inventoryBeeRing.setDrone(droneStack);
}
ItemStack temp = droneStack.copy();
temp.stackSize = 1;
ringInventory.setInventorySlotContents(1, temp);
int current = bee.getHealth();
int max = bee.getMaxHealth();
ringInventory.setCurrentBeeHealth((current * 100) / max);
ringInventory.setCurrentBeeColour(bee.getGenome().getPrimary().getIconColour(0));
ItemStack queenStack = droneStack.copy();
queenStack.stackSize = 1;
inventoryBeeRing.setQueen(queenStack);
int beeHealth = bee.getHealth();
int maxBeeHealth = bee.getMaxHealth();
inventoryBeeRing.setCurrentBeeHealth((beeHealth * 100) / maxBeeHealth);
inventoryBeeRing.setCurrentBeeColour(bee.getGenome().getPrimary().getIconColour(0));
}
}
private boolean hasDrone(ItemStack itemStack, EntityPlayer player, int slot, InventoryBeeRing ringInventory) {
return getInventory(player, itemStack, slot, true).getStackInSlot(DRONE_SLOT) != null;
}
private void tickQueen(EntityPlayer player, InventoryBeeRing inventoryBeeRing) {
ItemStack queenStack = inventoryBeeRing.getQueen();
IBee queen = BeeManager.beeRoot.getMember(queenStack);
private void tickQueen(ItemStack itemStack, EntityPlayer player, int slot, InventoryBeeRing ringInventory) {
inventoryBeeRing.setCurrentBeeHealth((queen.getHealth() * 100) / queen.getMaxHealth());
inventoryBeeRing.setCurrentBeeColour(queen.getGenome().getPrimary().getIconColour(0));
IEffectData[][] effects = getPlayerEffects(player);
int index = inventoryBeeRing.getRingSlotIndex();
RingHousing housingLogic = new RingHousing(player, inventoryBeeRing);
effects[index] = queen.doEffect(effects[index], housingLogic);
if (player.worldObj.isRemote && player.worldObj.getWorldTime() % 5 == 0) {
effects[index] = queen.doFX(effects[index], housingLogic);
}
setPlayerEffects(player, effects);
IBee queen = magicbees.bees.BeeManager.beeRoot.getMember(ringInventory.getStackInSlot(QUEEN_SLOT));
ringInventory.setCurrentBeeHealth((queen.getHealth() * 100) / queen.getMaxHealth());
ringInventory.setCurrentBeeColour(queen.getGenome().getPrimary().getIconColour(0));
RingHousing housingLogic = new RingHousing(player, ringInventory);
ringInventory.setEffectAndInitialize(queen);
ringInventory.effectData = queen.doEffect(ringInventory.effectData, housingLogic);
ringInventory.writeEffectNBT();
// Crashes irregularly for some reason I can't figure out, but this is just the bee effects around you, so it's
// not a huge deal. It would be nice to have though
// I will get around to it later. (Crash has something to do with entityFX when the player is moving)
// if (player.getEntityWorld().getWorldTime() % 5 == 0) {
// IBR.effectData = queen.doFX(IBR.effectData, housingLogic);
// }
// run the queen
if (ringInventory.throttle > 550) {
ringInventory.setThrottle(0);
queen.age(player.getEntityWorld(), 0.26f);
int throttle = inventoryBeeRing.getThrottle();
if (throttle > 550) {
inventoryBeeRing.setThrottle(0);
queen.age(player.worldObj, 0.26f);
if (queen.getHealth() == 0) {
ringInventory.setInventorySlotContents(1, null);
ringInventory.setCurrentBeeHealth(0);
ringInventory.setCurrentBeeColour(0x0ffffff);
inventoryBeeRing.setQueen(null);
inventoryBeeRing.setCurrentBeeHealth(0);
inventoryBeeRing.setCurrentBeeColour(0x0ffffff);
} else {
queen.writeToNBT(ringInventory.contents[QUEEN_SLOT].stackTagCompound);
queen.writeToNBT(queenStack.getTagCompound());
inventoryBeeRing.setQueen(queenStack);
}
} else {
ringInventory.setThrottle(ringInventory.throttle + 1);
inventoryBeeRing.setThrottle(throttle + 1);
}
}
private boolean hasQueen(ItemStack itemStack, EntityPlayer player, int slot, InventoryBeeRing ringInventory) {
return ringInventory.getStackInSlot(QUEEN_SLOT) != null;
private static IEffectData[][] getPlayerEffects(EntityPlayer player) {
UUID id = player.getUniqueID();
return PLAYER_EFFECTS.containsKey(id) ? PLAYER_EFFECTS.get(id)
: new IEffectData[][] { new IEffectData[2], new IEffectData[2] };
}
private static void setPlayerEffects(EntityPlayer player, IEffectData[][] effects) {
PLAYER_EFFECTS.put(player.getUniqueID(), effects);
}
@Override
public void onEquipped(ItemStack itemStack, EntityLivingBase entityLivingBase) {
if (entityLivingBase instanceof EntityPlayer) {
EntityPlayer player = (EntityPlayer) entityLivingBase;
InventoryBeeRing inventoryBeeRing = new InventoryBeeRing(itemStack, player);
InventoryBaubles baubles = PlayerHandler.getPlayerBaubles(player);
for (int i = 0; i < baubles.getSizeInventory(); i++) {
if (baubles.getStackInSlot(i) == itemStack) {
inventoryBeeRing.setRingSlotIndex(i == 1 ? 0 : 1);
break;
}
}
}
}
@Override
public void onUnequipped(ItemStack itemStack, EntityLivingBase entityLivingBase) {
}
public void onUnequipped(ItemStack itemStack, EntityLivingBase entityLivingBase) {}
@Override
public boolean canEquip(ItemStack itemStack, EntityLivingBase entityLivingBase) {
@@ -196,9 +184,4 @@ public class ItemBeeRing extends Item implements IBauble {
public boolean canUnequip(ItemStack itemStack, EntityLivingBase entityLivingBase) {
return true;
}
public static InventoryBeeRing getInventory(EntityPlayer player, ItemStack itemStack, int slot,
boolean baubleFlag) {
return new InventoryBeeRing(itemStack, player, slot, baubleFlag);
}
}
@@ -1,306 +1,110 @@
package magicbees.itemInventories;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import baubles.common.container.InventoryBaubles;
import baubles.common.lib.PlayerHandler;
import forestry.api.apiculture.EnumBeeChromosome;
import forestry.api.apiculture.IAlleleBeeEffect;
import forestry.api.apiculture.IBee;
import forestry.api.genetics.IEffectData;
import forestry.core.inventory.ItemInventory;
public class InventoryBeeRing implements IInventory {
public class InventoryBeeRing extends ItemInventory {
public EntityPlayer player;
public ItemStack parent;
public ItemStack[] contents;
public int itemLocatedSlot;
public boolean baubleFlag;
public IEffectData[] effectData = new IEffectData[2];
private static final String KEY_SLOT_INDEX = "SlotIndex";
private static final String KEY_HEALTH = "Health";
private static final String KEY_THROTTLE = "Throttle";
private static final String KEY_COLOUR = "Colour";
public int currentBeeHealth;
public int currentBeeColour;
public int throttle;
private static final int DRONE_SLOT = 0;
private static final int QUEEN_SLOT = 1;
private final static String KEY_SLOTS = "Slots";
private final static String KEY_HEALTH = "Health";
private final static String KEY_THROTTLE = "Throttle";
private int ringSlotIndex;
private int currentBeeHealth;
private int currentBeeColour;
private int throttle;
private final static String KEY_EFFECT_1 = "Effect1";
private final static String KEY_EFFECT_2 = "Effect2";
private final static String KEY_COLOUR = "Colour";
public InventoryBeeRing(ItemStack stack, EntityPlayer player, int slot, boolean baubleFlag) {
this.parent = stack;
this.player = player;
this.contents = new ItemStack[2];
this.itemLocatedSlot = slot;
this.baubleFlag = baubleFlag;
readFromNBT(stack.getTagCompound());
public InventoryBeeRing(ItemStack stack, EntityPlayer player) {
super(player, 2, stack);
}
@Override
public void readFromNBT(NBTTagCompound compound) {
if (compound == null) {
return;
}
// Initializing slot contents
if (compound.hasKey(KEY_SLOTS)) {
NBTTagCompound nbtSlots = compound.getCompoundTag(KEY_SLOTS);
for (int i = 0; i < contents.length; i++) {
String slotKey = getSlotNBTKey(i);
if (nbtSlots.hasKey(slotKey)) {
NBTTagCompound itemNbt = nbtSlots.getCompoundTag(slotKey);
ItemStack itemStack = ItemStack.loadItemStackFromNBT(itemNbt);
contents[i] = itemStack;
} else {
contents[i] = null;
}
}
}
// Initializing Throttle for logic and Health, Colour for GUI effects
if (compound.getInteger(KEY_THROTTLE) != 0) {
throttle = compound.getInteger(KEY_THROTTLE);
}
if (compound.getInteger(KEY_HEALTH) != 0) {
currentBeeHealth = compound.getInteger(KEY_HEALTH);
}
if (compound.getInteger(KEY_COLOUR) != 0) {
currentBeeColour = compound.getInteger(KEY_COLOUR);
}
}
// Helper method for NBT Keys
private static String getSlotNBTKey(int i) {
return Integer.toString(i, Character.MAX_RADIX);
}
@Override
public int getSizeInventory() {
return contents.length;
}
@Override
public ItemStack getStackInSlot(int slot) {
return contents[slot];
}
@Override
public ItemStack decrStackSize(int slot, int count) {
ItemStack stack = getStackInSlot(slot);
if (stack == null) {
return null;
}
if (stack.stackSize <= count) {
setInventorySlotContents(slot, null);
return stack;
} else {
ItemStack product = stack.splitStack(count);
setInventorySlotContents(slot, stack);
return product;
}
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
ItemStack toReturn = getStackInSlot(slot);
if (toReturn != null) {
setInventorySlotContents(slot, null);
}
return toReturn;
}
// sets the internal inventory of this class to the provided item stack and writes that to the NBT of the parent
// (the ring)
@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
if (stack != null && stack.stackSize == 0) {
stack = null;
}
contents[slot] = stack;
ItemStack parent = this.parent;
NBTTagCompound nbt = parent.getTagCompound();
if (nbt == null) {
nbt = new NBTTagCompound();
parent.setTagCompound(nbt);
}
NBTTagCompound slotNbt;
if (!nbt.hasKey(KEY_SLOTS)) {
slotNbt = new NBTTagCompound();
nbt.setTag(KEY_SLOTS, slotNbt);
} else {
slotNbt = nbt.getCompoundTag(KEY_SLOTS);
}
String slotKey = getSlotNBTKey(slot);
if (stack == null) {
slotNbt.removeTag(slotKey);
} else {
NBTTagCompound itemNbt = new NBTTagCompound();
stack.writeToNBT(itemNbt);
slotNbt.setTag(slotKey, itemNbt);
}
setInventorySlot(parent);
}
// private method to manually set the inventory of a player
private void setInventorySlot(ItemStack parent) {
if (this.player.getHealth() == 0) {
return;
}
if (baubleFlag) {
InventoryBaubles baubles = PlayerHandler.getPlayerBaubles(this.player);
baubles.setInventorySlotContents(itemLocatedSlot, parent);
} else {
player.inventory.setInventorySlotContents(itemLocatedSlot, parent);
}
}
// Writes the effectData field to NBT in a readable way (This and the next method could probably be condensed, but
// I feel that this is the most readable form I could come up with.
public void writeEffectNBT() {
NBTTagCompound compound = parent.getTagCompound();
if (compound == null) {
compound = new NBTTagCompound();
parent.setTagCompound(compound);
getParent().setTagCompound(compound);
}
NBTTagCompound slotNbt1;
if (!compound.hasKey(KEY_EFFECT_1)) {
slotNbt1 = new NBTTagCompound();
compound.setTag(KEY_EFFECT_1, slotNbt1);
} else {
slotNbt1 = compound.getCompoundTag(KEY_EFFECT_1);
super.readFromNBT(compound);
if (compound.hasKey(KEY_SLOT_INDEX)) {
this.ringSlotIndex = compound.getInteger(KEY_SLOT_INDEX);
}
NBTTagCompound slotNbt2;
if (!compound.hasKey(KEY_EFFECT_2)) {
slotNbt2 = new NBTTagCompound();
compound.setTag(KEY_EFFECT_2, slotNbt2);
} else {
slotNbt2 = compound.getCompoundTag(KEY_EFFECT_2);
if (compound.hasKey(KEY_HEALTH)) {
this.currentBeeHealth = compound.getInteger(KEY_HEALTH);
}
for (int i = 0; i < 5; i++) {
String slotKey = getSlotNBTKey(i);
try {
slotNbt1.setInteger(slotKey + "i", effectData[0].getInteger(i));
slotNbt1.setBoolean(slotKey + "b", effectData[0].getBoolean(i));
slotNbt1.setFloat(slotKey + "f", effectData[0].getFloat(i));
} catch (Exception ignored) {}
if (compound.hasKey(KEY_COLOUR)) {
this.currentBeeColour = compound.getInteger(KEY_COLOUR);
}
for (int i = 0; i < 5; i++) {
String slotKey = getSlotNBTKey(i);
try {
slotNbt2.setInteger(slotKey + "i", effectData[1].getInteger(i));
slotNbt2.setBoolean(slotKey + "b", effectData[1].getBoolean(i));
slotNbt2.setFloat(slotKey + "f", effectData[1].getFloat(i));
} catch (Exception ignored) {}
}
setInventorySlot(parent);
}
// Creates the correct implementation of IEffectData and pulls the data saved in the NBT into the effectData field
public void setEffectAndInitialize(IBee queen) {
effectData[0] = queen.getGenome().getEffect().validateStorage(effectData[0]);
effectData[1] = ((IAlleleBeeEffect) queen.getGenome().getInactiveAllele(EnumBeeChromosome.EFFECT))
.validateStorage(effectData[1]);
NBTTagCompound compound = parent.getTagCompound();
if (compound.hasKey(KEY_EFFECT_1)) {
NBTTagCompound nbtSlots1 = compound.getCompoundTag(KEY_EFFECT_1);
for (int i = 0; i < 5; i++) {
String slotKey = getSlotNBTKey(i);
if (nbtSlots1.hasKey(slotKey + "i")) {
int effectNBTInt = nbtSlots1.getInteger(slotKey + "i");
try {
effectData[0].setInteger(i, effectNBTInt);
} catch (Exception ignored) {}
}
if (nbtSlots1.hasKey(slotKey + "b")) {
boolean effectNBTBool = nbtSlots1.getBoolean(slotKey + "b");
try {
effectData[0].setBoolean(i, effectNBTBool);
} catch (Exception ignored) {}
}
if (nbtSlots1.hasKey(slotKey + "f")) {
float effectNBTFloat = nbtSlots1.getFloat(slotKey + "f");
try {
effectData[0].setFloat(i, effectNBTFloat);
} catch (Exception ignored) {}
}
}
}
if (compound.hasKey(KEY_EFFECT_2)) {
NBTTagCompound nbtSlots2 = compound.getCompoundTag(KEY_EFFECT_2);
for (int i = 0; i < 5; i++) {
String slotKey = getSlotNBTKey(i);
if (nbtSlots2.hasKey(slotKey + "i")) {
int effectNBTInt = nbtSlots2.getInteger(slotKey + "i");
try {
effectData[1].setInteger(i, effectNBTInt);
} catch (Exception ignored) {}
}
if (nbtSlots2.hasKey(slotKey + "b")) {
boolean effectNBTBool = nbtSlots2.getBoolean(slotKey + "b");
try {
effectData[1].setBoolean(i, effectNBTBool);
} catch (Exception ignored) {}
}
if (nbtSlots2.hasKey(slotKey + "f")) {
float effectNBTFloat = nbtSlots2.getFloat(slotKey + "f");
try {
effectData[1].setFloat(i, effectNBTFloat);
} catch (Exception ignored) {}
}
}
if (compound.hasKey(KEY_THROTTLE)) {
this.throttle = compound.getInteger(KEY_THROTTLE);
}
}
// Setters for logic and GUI display info
public void setThrottle(int throttle) {
this.throttle = throttle;
this.parent.getTagCompound().setInteger(KEY_THROTTLE, this.throttle);
setInventorySlot(parent);
public boolean hasDrone() {
return this.getStackInSlot(DRONE_SLOT) != null;
}
public ItemStack getDrone() {
return this.getStackInSlot(DRONE_SLOT);
}
public void setDrone(ItemStack droneStack) {
this.setInventorySlotContents(DRONE_SLOT, droneStack);
}
public boolean hasQueen() {
return this.getStackInSlot(QUEEN_SLOT) != null;
}
public ItemStack getQueen() {
return this.getStackInSlot(QUEEN_SLOT);
}
public void setQueen(ItemStack queenStack) {
this.setInventorySlotContents(QUEEN_SLOT, queenStack);
}
public int getRingSlotIndex() {
return this.ringSlotIndex;
}
public void setRingSlotIndex(int ringSlotIndex) {
this.ringSlotIndex = ringSlotIndex;
getParent().getTagCompound().setInteger(KEY_SLOT_INDEX, this.ringSlotIndex);
}
public int getCurrentBeeHealth() {
return this.currentBeeHealth;
}
public void setCurrentBeeHealth(int health) {
this.currentBeeHealth = health;
this.parent.getTagCompound().setInteger(KEY_HEALTH, this.currentBeeHealth);
setInventorySlot(parent);
getParent().getTagCompound().setInteger(KEY_HEALTH, this.currentBeeHealth);
}
public int getCurrentBeeColour() {
return this.currentBeeColour;
}
public void setCurrentBeeColour(int colour) {
this.currentBeeColour = colour;
this.parent.getTagCompound().setInteger(KEY_COLOUR, this.currentBeeColour);
getParent().getTagCompound().setInteger(KEY_COLOUR, this.currentBeeColour);
}
public int getThrottle() {
return this.throttle;
}
public void setThrottle(int throttle) {
this.throttle = throttle;
getParent().getTagCompound().setInteger(KEY_THROTTLE, this.throttle);
}
@Override
@@ -312,28 +116,4 @@ public class InventoryBeeRing implements IInventory {
public boolean hasCustomInventoryName() {
return false;
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public void markDirty() {}
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
return true;
}
@Override
public void openInventory() {}
@Override
public void closeInventory() {}
@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
return true;
}
}
@@ -35,10 +35,10 @@ public class RingHousing implements IBeeHousing {
private static final Iterable<IBeeListener> beeListeners = ImmutableSet.of(new DefaultBeeListener());
private static final Iterable<IBeeModifier> beeModifiers = ImmutableSet.of(new RingHousing.RingHousingModifier());
public RingHousing(EntityPlayer player, InventoryBeeRing IBR) {
public RingHousing(EntityPlayer player, InventoryBeeRing inventoryBeeRing) {
this.player = player;
this.biome = player.getEntityWorld().getBiomeGenForCoords((int) player.posX, (int) player.posY);
this.inventory = new RingBeeHousingInventory(IBR);
this.inventory = new RingBeeHousingInventory(inventoryBeeRing);
this.beekeepingLogic = BeeManager.beeRoot.createBeekeepingLogic(this);
}
@@ -115,15 +115,15 @@ public class RingHousing implements IBeeHousing {
private static class RingBeeHousingInventory implements IBeeHousingInventory {
private final InventoryBeeRing IBR;
private final InventoryBeeRing inventoryBeeRing;
public RingBeeHousingInventory(InventoryBeeRing IBR) {
this.IBR = IBR;
this.inventoryBeeRing = IBR;
}
@Override
public ItemStack getQueen() {
return IBR.getStackInSlot(1);
return inventoryBeeRing.getQueen();
}
@Override
@@ -133,7 +133,7 @@ public class RingHousing implements IBeeHousing {
@Override
public void setQueen(ItemStack itemStack) {
IBR.setInventorySlotContents(1, itemStack);
inventoryBeeRing.setQueen(itemStack);
}
@Override