Miscellaneous setup
This commit is contained in:
@@ -3,25 +3,36 @@ package electroblob.wizardry.tileentity;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import electroblob.wizardry.WizardData;
|
||||
import electroblob.wizardry.Wizardry;
|
||||
import electroblob.wizardry.constants.Constants;
|
||||
import electroblob.wizardry.constants.Tier;
|
||||
import electroblob.wizardry.event.SpellBindEvent;
|
||||
import electroblob.wizardry.item.ItemArcaneTome;
|
||||
import electroblob.wizardry.item.ItemArmourUpgrade;
|
||||
import electroblob.wizardry.item.ItemSpellBook;
|
||||
import electroblob.wizardry.item.ItemWand;
|
||||
import electroblob.wizardry.item.ItemWizardArmour;
|
||||
import electroblob.wizardry.registry.Spells;
|
||||
import electroblob.wizardry.registry.WizardryAchievements;
|
||||
import electroblob.wizardry.registry.WizardryItems;
|
||||
import electroblob.wizardry.spell.Spell;
|
||||
import electroblob.wizardry.util.WandHelper;
|
||||
import electroblob.wizardry.util.WizardryUtilities;
|
||||
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.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
|
||||
public class ContainerArcaneWorkbench extends Container {
|
||||
|
||||
public TileEntityArcaneWorkbench tileEntityArcaneWorkbench;
|
||||
/** The arcane workbench tile entity associated with this container. */
|
||||
public TileEntityArcaneWorkbench tileentity;
|
||||
|
||||
public static final ResourceLocation EMPTY_SLOT_CRYSTAL = new ResourceLocation(Wizardry.MODID, "gui/empty_slot_crystal");
|
||||
public static final ResourceLocation EMPTY_SLOT_UPGRADE = new ResourceLocation(Wizardry.MODID, "gui/empty_slot_upgrade");
|
||||
@@ -29,6 +40,7 @@ public class ContainerArcaneWorkbench extends Container {
|
||||
public static final int CRYSTAL_SLOT = 8;
|
||||
public static final int WAND_SLOT = 9;
|
||||
public static final int UPGRADE_SLOT = 10;
|
||||
|
||||
private static final int[][][] SPELL_BOOK_SLOT_COORDS = {
|
||||
{{80, 22}, {121, 51}, {106, 98}, {54, 98}, {39, 51}, {-999, -999}, {-999, -999}, {-999, -999}},
|
||||
{{80, 22}, {117, 43}, {117, 85}, {80, 106}, {43, 85}, {43, 43}, {-999, -999}, {-999, -999}},
|
||||
@@ -38,7 +50,7 @@ public class ContainerArcaneWorkbench extends Container {
|
||||
|
||||
public ContainerArcaneWorkbench(IInventory inventory, TileEntityArcaneWorkbench tileentity){
|
||||
|
||||
this.tileEntityArcaneWorkbench = tileentity;
|
||||
this.tileentity = tileentity;
|
||||
|
||||
ItemStack wand = tileentity.getStackInSlot(WAND_SLOT);
|
||||
|
||||
@@ -47,16 +59,16 @@ public class ContainerArcaneWorkbench extends Container {
|
||||
}
|
||||
|
||||
this.addSlotToContainer(new SlotItemList(tileentity, CRYSTAL_SLOT, 8, 88, 64, WizardryItems.magic_crystal))
|
||||
.setBackgroundName(EMPTY_SLOT_CRYSTAL.toString());
|
||||
|
||||
.setBackgroundName(EMPTY_SLOT_CRYSTAL.toString());
|
||||
|
||||
this.addSlotToContainer(new SlotWandArmour(tileentity, WAND_SLOT, 80, 64, this));
|
||||
|
||||
Set<Item> upgrades = new HashSet<Item>(WandHelper.getSpecialUpgrades());
|
||||
|
||||
Set<Item> upgrades = new HashSet<Item>(WandHelper.getSpecialUpgrades()); // Can't be done statically.
|
||||
upgrades.add(WizardryItems.arcane_tome);
|
||||
upgrades.add(WizardryItems.armour_upgrade);
|
||||
|
||||
|
||||
this.addSlotToContainer(new SlotItemList(tileentity, UPGRADE_SLOT, 8, 106, 1, upgrades.toArray(new Item[0])))
|
||||
.setBackgroundName(EMPTY_SLOT_UPGRADE.toString());
|
||||
.setBackgroundName(EMPTY_SLOT_UPGRADE.toString());
|
||||
|
||||
for(int x = 0; x < 9; x++){
|
||||
this.addSlotToContainer(new Slot(inventory, x, 8 + x * 18, 196));
|
||||
@@ -72,17 +84,14 @@ public class ContainerArcaneWorkbench extends Container {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canInteractWith(EntityPlayer par1EntityPlayer)
|
||||
{
|
||||
return this.tileEntityArcaneWorkbench.isUseableByPlayer(par1EntityPlayer);
|
||||
public boolean canInteractWith(EntityPlayer player){
|
||||
return this.tileentity.isUseableByPlayer(player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called from the central wand/armour slot when its item is changed or removed.
|
||||
*/
|
||||
// I wrote this!
|
||||
/** Called from the central wand/armour slot when its item is changed or removed. */
|
||||
// In case I forget again and think it should have @Override: I wrote this!
|
||||
public void onSlotChanged(int slotNumber, ItemStack stack, EntityPlayer player){
|
||||
|
||||
|
||||
if(slotNumber == WAND_SLOT){
|
||||
|
||||
if(stack == null || (!(stack.getItem() instanceof ItemWand) && stack.getItem() != WizardryItems.blank_scroll)){
|
||||
@@ -110,8 +119,8 @@ public class ContainerArcaneWorkbench extends Container {
|
||||
if(stack.getItem() == WizardryItems.blank_scroll){
|
||||
// If a blank scroll is added
|
||||
// The first slot is shown
|
||||
this.getSlot(0).xDisplayPosition = ContainerArcaneWorkbench.SPELL_BOOK_SLOT_COORDS[0][0][0];
|
||||
this.getSlot(0).yDisplayPosition = ContainerArcaneWorkbench.SPELL_BOOK_SLOT_COORDS[0][0][1];
|
||||
this.getSlot(0).xDisplayPosition = SPELL_BOOK_SLOT_COORDS[0][0][0];
|
||||
this.getSlot(0).yDisplayPosition = SPELL_BOOK_SLOT_COORDS[0][0][1];
|
||||
|
||||
// The rest of the slots are hidden
|
||||
for(int i=1; i<CRYSTAL_SLOT; i++){
|
||||
@@ -138,7 +147,7 @@ public class ContainerArcaneWorkbench extends Container {
|
||||
for(int i=0; i<CRYSTAL_SLOT; i++){
|
||||
|
||||
int n = WandHelper.getUpgradeLevel(stack, WizardryItems.attunement_upgrade);
|
||||
int[] coords = ContainerArcaneWorkbench.SPELL_BOOK_SLOT_COORDS[n][i];
|
||||
int[] coords = SPELL_BOOK_SLOT_COORDS[n][i];
|
||||
|
||||
Slot slot1 = this.getSlot(i);
|
||||
// Puts the slot back in the correct position
|
||||
@@ -165,56 +174,49 @@ public class ContainerArcaneWorkbench extends Container {
|
||||
|
||||
// FIXME: It only seems to be syncing correctly when a stack is put into the slot, not taken out.
|
||||
// Was this broken in 1.7.10 as well?
|
||||
this.tileEntityArcaneWorkbench.sync();
|
||||
this.tileentity.sync();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int clickedSlotId)
|
||||
public ItemStack transferStackInSlot(EntityPlayer player, int clickedSlotId)
|
||||
{
|
||||
ItemStack remainder = null;
|
||||
Slot slot = (Slot)this.inventorySlots.get(clickedSlotId);
|
||||
|
||||
if (slot != null && slot.getHasStack())
|
||||
{
|
||||
ItemStack itemstack = slot.getStack(); // The stack that was there originally
|
||||
remainder = itemstack.copy(); // A copy of that stack
|
||||
ItemStack stack = slot.getStack(); // The stack that was there originally
|
||||
remainder = stack.copy(); // A copy of that stack
|
||||
|
||||
// Workbench -> inventory
|
||||
if(clickedSlotId <= UPGRADE_SLOT){
|
||||
// Tries to move the stack into the player's inventory. If this fails...
|
||||
if (!this.mergeItemStack(itemstack, UPGRADE_SLOT + 1, this.inventorySlots.size(), true)){
|
||||
if (!this.mergeItemStack(stack, UPGRADE_SLOT + 1, this.inventorySlots.size(), true)){
|
||||
return null; // ...nothing else happens.
|
||||
}
|
||||
|
||||
// Inventory -> workbench
|
||||
}else{
|
||||
}
|
||||
// Inventory -> workbench
|
||||
else{
|
||||
// The following logic prevents shift-clicking transferring the items to the wrong slot.
|
||||
int minSlotId = 0;
|
||||
int maxSlotId = UPGRADE_SLOT;
|
||||
|
||||
if(itemstack.getItem() instanceof ItemSpellBook){
|
||||
if(stack.getItem() instanceof ItemSpellBook){
|
||||
minSlotId = 0;
|
||||
maxSlotId = CRYSTAL_SLOT-1;
|
||||
}
|
||||
else if(itemstack.getItem() == WizardryItems.magic_crystal){
|
||||
else if(stack.getItem() == WizardryItems.magic_crystal){
|
||||
minSlotId = CRYSTAL_SLOT;
|
||||
maxSlotId = CRYSTAL_SLOT;
|
||||
}
|
||||
else if(itemstack.getItem() instanceof ItemWand || itemstack.getItem() instanceof ItemWizardArmour
|
||||
|| itemstack.getItem() == WizardryItems.blank_scroll){
|
||||
else if(stack.getItem() instanceof ItemWand || stack.getItem() instanceof ItemWizardArmour
|
||||
|| stack.getItem() == WizardryItems.blank_scroll){
|
||||
minSlotId = WAND_SLOT;
|
||||
maxSlotId = WAND_SLOT;
|
||||
}
|
||||
else if(itemstack.getItem() instanceof ItemArcaneTome
|
||||
|| itemstack.getItem() instanceof ItemArmourUpgrade
|
||||
|| itemstack.getItem() == WizardryItems.condenser_upgrade
|
||||
|| itemstack.getItem() == WizardryItems.siphon_upgrade
|
||||
|| itemstack.getItem() == WizardryItems.range_upgrade
|
||||
|| itemstack.getItem() == WizardryItems.cooldown_upgrade
|
||||
|| itemstack.getItem() == WizardryItems.duration_upgrade
|
||||
|| itemstack.getItem() == WizardryItems.storage_upgrade
|
||||
|| itemstack.getItem() == WizardryItems.blast_upgrade
|
||||
|| itemstack.getItem() == WizardryItems.attunement_upgrade){
|
||||
else if(stack.getItem() instanceof ItemArcaneTome
|
||||
|| stack.getItem() instanceof ItemArmourUpgrade
|
||||
|| WandHelper.isWandUpgrade(stack.getItem())){
|
||||
minSlotId = UPGRADE_SLOT;
|
||||
maxSlotId = UPGRADE_SLOT;
|
||||
}
|
||||
@@ -222,13 +224,13 @@ public class ContainerArcaneWorkbench extends Container {
|
||||
return null; // If none of the above cases were true, then the item won't fit in the workbench.
|
||||
}
|
||||
|
||||
if(!this.mergeItemStack(itemstack, minSlotId, maxSlotId + 1, false))
|
||||
if(!this.mergeItemStack(stack, minSlotId, maxSlotId + 1, false))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if (itemstack.stackSize == 0)
|
||||
if (stack.stackSize == 0)
|
||||
{
|
||||
slot.putStack((ItemStack)null);
|
||||
}
|
||||
@@ -237,12 +239,12 @@ public class ContainerArcaneWorkbench extends Container {
|
||||
slot.onSlotChanged();
|
||||
}
|
||||
|
||||
if (itemstack.stackSize == remainder.stackSize)
|
||||
if (stack.stackSize == remainder.stackSize)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
slot.onPickupFromSlot(par1EntityPlayer, itemstack);
|
||||
slot.onPickupFromSlot(player, stack);
|
||||
}
|
||||
|
||||
return remainder;
|
||||
@@ -262,4 +264,198 @@ public class ContainerArcaneWorkbench extends Container {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Called (via {@link electroblob.wizardry.packet.PacketControlInput PacketControlInput}) when the apply button
|
||||
* in the arcane workbench GUI is pressed. */
|
||||
// All operations on the items contained in the inventory simply call the corresponding methods in the tileentity.
|
||||
// As of 2.1, for the sake of events and neatness of code, this was moved here from TileEntityArcaneWorkbench.
|
||||
public void onApplyButtonPressed(EntityPlayer player){
|
||||
|
||||
ItemStack wand = this.getSlot(WAND_SLOT).getStack();
|
||||
ItemStack[] spellBooks = new ItemStack[CRYSTAL_SLOT];
|
||||
for(int i=0; i<spellBooks.length; i++){
|
||||
spellBooks[i] = this.getSlot(i).getStack();
|
||||
}
|
||||
ItemStack crystals = this.getSlot(CRYSTAL_SLOT).getStack();
|
||||
ItemStack upgrade = this.getSlot(UPGRADE_SLOT).getStack();
|
||||
|
||||
if(MinecraftForge.EVENT_BUS.post(new SpellBindEvent(player, this))) return;
|
||||
|
||||
// Since the workbench now accepts armour as well as wands, this check is needed.
|
||||
if(wand != null && wand.getItem() instanceof ItemWand){
|
||||
|
||||
// Upgrades wand if necessary. Damage is copied, preserving remaining durability,
|
||||
// and also the entire NBT tag compound.
|
||||
if(upgrade != null){
|
||||
|
||||
if(upgrade.getItem() == WizardryItems.arcane_tome){
|
||||
|
||||
ItemStack newWand;
|
||||
|
||||
switch(Tier.values()[upgrade.getItemDamage()]){
|
||||
|
||||
case APPRENTICE:
|
||||
if(((ItemWand)wand.getItem()).tier == Tier.BASIC){
|
||||
newWand = new ItemStack(WizardryUtilities.getWand(Tier.values()[upgrade.getItemDamage()], ((ItemWand)wand.getItem()).element));
|
||||
newWand.setTagCompound(wand.getTagCompound());
|
||||
// This needs to be done after copying the tag compound so the max damage for the new wand takes storage
|
||||
// upgrades into account.
|
||||
newWand.setItemDamage(newWand.getMaxDamage() - (wand.getMaxDamage() - wand.getItemDamage()));
|
||||
this.putStackInSlot(WAND_SLOT, newWand);
|
||||
this.putStackInSlot(UPGRADE_SLOT, null);
|
||||
player.addStat(WizardryAchievements.apprentice, 1);
|
||||
}
|
||||
break;
|
||||
|
||||
case ADVANCED:
|
||||
if(((ItemWand)wand.getItem()).tier == Tier.APPRENTICE){
|
||||
newWand = new ItemStack(WizardryUtilities.getWand(Tier.values()[upgrade.getItemDamage()], ((ItemWand)wand.getItem()).element));
|
||||
newWand.setTagCompound(wand.getTagCompound());
|
||||
newWand.setItemDamage(newWand.getMaxDamage() - (wand.getMaxDamage() - wand.getItemDamage()));
|
||||
this.putStackInSlot(WAND_SLOT, newWand);
|
||||
this.putStackInSlot(UPGRADE_SLOT, null);
|
||||
}
|
||||
break;
|
||||
|
||||
case MASTER:
|
||||
if(((ItemWand)wand.getItem()).tier == Tier.ADVANCED){
|
||||
newWand = new ItemStack(WizardryUtilities.getWand(Tier.values()[upgrade.getItemDamage()], ((ItemWand)wand.getItem()).element));
|
||||
newWand.setTagCompound(wand.getTagCompound());
|
||||
newWand.setItemDamage(newWand.getMaxDamage() - (wand.getMaxDamage() - wand.getItemDamage()));
|
||||
this.putStackInSlot(WAND_SLOT, newWand);
|
||||
this.putStackInSlot(UPGRADE_SLOT, null);
|
||||
player.addStat(WizardryAchievements.master, 1);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// This needs to happen so the charging works on the new wand, not the old one.
|
||||
wand = this.getSlot(WAND_SLOT).getStack();
|
||||
|
||||
}else{
|
||||
|
||||
// Special upgrades
|
||||
|
||||
// Used to preserve existing mana when upgrading storage rather than creating free mana.
|
||||
int prevMana = wand.getMaxDamage() - wand.getItemDamage();
|
||||
|
||||
if(WandHelper.getTotalUpgrades(wand) < ((ItemWand)wand.getItem()).tier.upgradeLimit
|
||||
&& WandHelper.getUpgradeLevel(wand, upgrade.getItem()) < Constants.UPGRADE_STACK_LIMIT){
|
||||
|
||||
WandHelper.applyUpgrade(wand, upgrade.getItem());
|
||||
|
||||
// Special behaviours for specific upgrades
|
||||
if(upgrade.getItem() == WizardryItems.storage_upgrade){
|
||||
wand.setItemDamage(wand.getMaxDamage() - prevMana);
|
||||
}
|
||||
if(upgrade.getItem() == WizardryItems.attunement_upgrade){
|
||||
|
||||
Spell[] spells = WandHelper.getSpells(wand);
|
||||
Spell[] newSpells = new Spell[5 + WandHelper.getUpgradeLevel(wand, WizardryItems.attunement_upgrade)];
|
||||
|
||||
for(int i=0; i<newSpells.length; i++){
|
||||
// Prevents both NPEs and AIOOBEs
|
||||
newSpells[i] = i < spells.length && spells[i] != null ? spells[i] : Spells.none;
|
||||
}
|
||||
|
||||
WandHelper.setSpells(wand, newSpells);
|
||||
|
||||
int[] cooldown = WandHelper.getCooldowns(wand);
|
||||
int[] newCooldown = new int[5 + WandHelper.getUpgradeLevel(wand, WizardryItems.attunement_upgrade)];
|
||||
|
||||
if(cooldown.length > 0){
|
||||
for(int i=0; i<cooldown.length; i++){
|
||||
newCooldown[i] = cooldown[i];
|
||||
}
|
||||
}
|
||||
|
||||
WandHelper.setCooldowns(wand, newCooldown);
|
||||
}
|
||||
|
||||
this.getSlot(UPGRADE_SLOT).decrStackSize(1);
|
||||
player.addStat(WizardryAchievements.special_upgrade, 1);
|
||||
|
||||
if(WandHelper.getTotalUpgrades(wand) == Tier.MASTER.upgradeLimit){
|
||||
player.addStat(WizardryAchievements.max_out_wand, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Reads NBT spell id array to variable, edits this, then writes it back to NBT.
|
||||
// Original spells are preserved; if a slot is left empty the existing spell binding will remain.
|
||||
// Accounts for spells which cannot be applied because they are above the wand's tier; these spells
|
||||
// will not bind but the existing spell in that slot will remain and other applicable spells will
|
||||
// be bound as normal, along with any upgrades and crystals.
|
||||
Spell[] spells = WandHelper.getSpells(wand);
|
||||
if(spells.length <= 0){
|
||||
// 5 here because if the spell array doesn't exist, the wand can't possibly have attunement upgrades
|
||||
spells = new Spell[5];
|
||||
}
|
||||
for(int i=0; i<spells.length; i++){
|
||||
if(spellBooks[i] != null && !(Spell.get(spellBooks[i].getItemDamage()).tier.level > ((ItemWand)wand.getItem()).tier.level)){
|
||||
spells[i] = Spell.get(spellBooks[i].getItemDamage());
|
||||
}
|
||||
}
|
||||
WandHelper.setSpells(wand, spells);
|
||||
|
||||
// Charges wand by appropriate amount
|
||||
if(crystals != null){
|
||||
int chargeDepleted = wand.getItemDamage();
|
||||
//System.out.println("Charge depleted: " + chargeDepleted);
|
||||
//System.out.println("Crystals found: " + crystals.stackSize);
|
||||
if(crystals.stackSize * Constants.MANA_PER_CRYSTAL < chargeDepleted){
|
||||
//System.out.println("charging");
|
||||
wand.setItemDamage(chargeDepleted - crystals.stackSize * Constants.MANA_PER_CRYSTAL);
|
||||
this.getSlot(CRYSTAL_SLOT).decrStackSize(crystals.stackSize);
|
||||
}else if(chargeDepleted != 0){
|
||||
//System.out.println((int)Math.ceil(((double)chargeDepleted)/50));
|
||||
this.getSlot(CRYSTAL_SLOT).decrStackSize((int)Math.ceil(((double)chargeDepleted)/Constants.MANA_PER_CRYSTAL));
|
||||
wand.setItemDamage(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Armour
|
||||
else if(wand != null && wand.getItem() instanceof ItemWizardArmour){
|
||||
// Applies legendary upgrade
|
||||
if(upgrade != null && upgrade.getItem() == WizardryItems.armour_upgrade){
|
||||
if(!wand.hasTagCompound()){
|
||||
wand.setTagCompound(new NBTTagCompound());
|
||||
}
|
||||
if(!wand.getTagCompound().hasKey("legendary")){
|
||||
wand.getTagCompound().setBoolean("legendary", true);
|
||||
this.putStackInSlot(UPGRADE_SLOT, null);
|
||||
player.addStat(WizardryAchievements.legendary);
|
||||
}
|
||||
}
|
||||
// Charges armour by appropriate amount
|
||||
if(crystals != null){
|
||||
int chargeDepleted = wand.getItemDamage();
|
||||
if(crystals.stackSize * Constants.MANA_PER_CRYSTAL < chargeDepleted){
|
||||
wand.setItemDamage(chargeDepleted - crystals.stackSize * Constants.MANA_PER_CRYSTAL);
|
||||
this.getSlot(CRYSTAL_SLOT).decrStackSize(crystals.stackSize);
|
||||
}else if(chargeDepleted != 0){
|
||||
this.getSlot(CRYSTAL_SLOT).decrStackSize((int)Math.ceil(((double)chargeDepleted)/Constants.MANA_PER_CRYSTAL));
|
||||
wand.setItemDamage(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Scrolls
|
||||
else if(wand != null && wand.getItem() == WizardryItems.blank_scroll){
|
||||
// Spells can only be bound to scrolls if the player has already cast them (prevents casting of master spells without getting a master wand)
|
||||
// This restriction does not apply in creative mode
|
||||
if(spellBooks[0] != null && (player.capabilities.isCreativeMode || (WizardData.get(player) != null
|
||||
&& WizardData.get(player).hasSpellBeenDiscovered(Spell.get(spellBooks[0].getItemDamage()))))
|
||||
&& crystals != null && crystals.stackSize * Constants.MANA_PER_CRYSTAL > Spell.get(spellBooks[0].getItemDamage()).cost){
|
||||
|
||||
this.getSlot(CRYSTAL_SLOT).decrStackSize((int)Math.ceil(((double)Spell.get(spellBooks[0].getItemDamage()).cost)/Constants.MANA_PER_CRYSTAL));
|
||||
this.putStackInSlot(WAND_SLOT, new ItemStack(WizardryItems.scroll, 1, spellBooks[0].getItemDamage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,18 +3,11 @@ package electroblob.wizardry.tileentity;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import electroblob.wizardry.WizardData;
|
||||
import electroblob.wizardry.constants.Constants;
|
||||
import electroblob.wizardry.constants.Tier;
|
||||
import electroblob.wizardry.item.ItemWand;
|
||||
import electroblob.wizardry.item.ItemWizardArmour;
|
||||
import electroblob.wizardry.registry.Spells;
|
||||
import electroblob.wizardry.registry.WizardryAchievements;
|
||||
import electroblob.wizardry.registry.WizardryBlocks;
|
||||
import electroblob.wizardry.registry.WizardryItems;
|
||||
import electroblob.wizardry.spell.Spell;
|
||||
import electroblob.wizardry.util.WandHelper;
|
||||
import electroblob.wizardry.util.WizardryUtilities;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
@@ -286,196 +279,4 @@ public class TileEntityArcaneWorkbench extends TileEntity implements IInventory,
|
||||
}
|
||||
}
|
||||
|
||||
/** Called (via {@link electroblob.wizardry.packet.PacketControlInput PacketControlInput}) when the apply button
|
||||
* in the arcane workbench GUI is pressed. */
|
||||
// IDEA: Perhaps this should be in the container class?
|
||||
public void onApplyButtonPressed(EntityPlayer player){
|
||||
|
||||
ItemStack wand = this.getStackInSlot(ContainerArcaneWorkbench.WAND_SLOT);
|
||||
ItemStack[] spellBooks = new ItemStack[ContainerArcaneWorkbench.CRYSTAL_SLOT];
|
||||
for(int i=0; i<spellBooks.length; i++){
|
||||
spellBooks[i] = this.getStackInSlot(i);
|
||||
}
|
||||
ItemStack crystals = this.getStackInSlot(ContainerArcaneWorkbench.CRYSTAL_SLOT);
|
||||
ItemStack upgrade = this.getStackInSlot(ContainerArcaneWorkbench.UPGRADE_SLOT);
|
||||
|
||||
// TODO: Implement this
|
||||
//if(MinecraftForge.EVENT_BUS.post(new SpellBindEvent(player))) return;
|
||||
|
||||
// Since the workbench now accepts armour as well as wands, this check is needed.
|
||||
if(wand != null && wand.getItem() instanceof ItemWand){
|
||||
|
||||
// Upgrades wand if necessary. Damage is copied, preserving remaining durability,
|
||||
// and also the entire NBT tag compound.
|
||||
if(upgrade != null){
|
||||
|
||||
if(upgrade.getItem() == WizardryItems.arcane_tome){
|
||||
|
||||
ItemStack newWand;
|
||||
|
||||
switch(Tier.values()[upgrade.getItemDamage()]){
|
||||
|
||||
case APPRENTICE:
|
||||
if(((ItemWand)wand.getItem()).tier == Tier.BASIC){
|
||||
newWand = new ItemStack(WizardryUtilities.getWand(Tier.values()[upgrade.getItemDamage()], ((ItemWand)wand.getItem()).element));
|
||||
newWand.setTagCompound(wand.getTagCompound());
|
||||
// This needs to be done after copying the tag compound so the max damage for the new wand takes storage
|
||||
// upgrades into account.
|
||||
newWand.setItemDamage(newWand.getMaxDamage() - (wand.getMaxDamage() - wand.getItemDamage()));
|
||||
this.setInventorySlotContents(ContainerArcaneWorkbench.WAND_SLOT, newWand);
|
||||
this.setInventorySlotContents(ContainerArcaneWorkbench.UPGRADE_SLOT, null);
|
||||
player.addStat(WizardryAchievements.apprentice, 1);
|
||||
}
|
||||
break;
|
||||
|
||||
case ADVANCED:
|
||||
if(((ItemWand)wand.getItem()).tier == Tier.APPRENTICE){
|
||||
newWand = new ItemStack(WizardryUtilities.getWand(Tier.values()[upgrade.getItemDamage()], ((ItemWand)wand.getItem()).element));
|
||||
newWand.setTagCompound(wand.getTagCompound());
|
||||
newWand.setItemDamage(newWand.getMaxDamage() - (wand.getMaxDamage() - wand.getItemDamage()));
|
||||
this.setInventorySlotContents(ContainerArcaneWorkbench.WAND_SLOT, newWand);
|
||||
this.setInventorySlotContents(ContainerArcaneWorkbench.UPGRADE_SLOT, null);
|
||||
}
|
||||
break;
|
||||
|
||||
case MASTER:
|
||||
if(((ItemWand)wand.getItem()).tier == Tier.ADVANCED){
|
||||
newWand = new ItemStack(WizardryUtilities.getWand(Tier.values()[upgrade.getItemDamage()], ((ItemWand)wand.getItem()).element));
|
||||
newWand.setTagCompound(wand.getTagCompound());
|
||||
newWand.setItemDamage(newWand.getMaxDamage() - (wand.getMaxDamage() - wand.getItemDamage()));
|
||||
this.setInventorySlotContents(ContainerArcaneWorkbench.WAND_SLOT, newWand);
|
||||
this.setInventorySlotContents(ContainerArcaneWorkbench.UPGRADE_SLOT, null);
|
||||
player.addStat(WizardryAchievements.master, 1);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// This needs to happen so the charging works on the new wand, not the old one.
|
||||
wand = this.getStackInSlot(ContainerArcaneWorkbench.WAND_SLOT);
|
||||
|
||||
}else{
|
||||
|
||||
// Special upgrades
|
||||
|
||||
// Used to preserve existing mana when upgrading storage rather than creating free mana.
|
||||
int prevMana = wand.getMaxDamage() - wand.getItemDamage();
|
||||
|
||||
if(WandHelper.getTotalUpgrades(wand) < ((ItemWand)wand.getItem()).tier.upgradeLimit
|
||||
&& WandHelper.getUpgradeLevel(wand, upgrade.getItem()) < Constants.UPGRADE_STACK_LIMIT){
|
||||
|
||||
WandHelper.applyUpgrade(wand, upgrade.getItem());
|
||||
|
||||
// Special behaviours for specific upgrades
|
||||
if(upgrade.getItem() == WizardryItems.storage_upgrade){
|
||||
wand.setItemDamage(wand.getMaxDamage() - prevMana);
|
||||
}
|
||||
if(upgrade.getItem() == WizardryItems.attunement_upgrade){
|
||||
|
||||
Spell[] spells = WandHelper.getSpells(wand);
|
||||
Spell[] newSpells = new Spell[5 + WandHelper.getUpgradeLevel(wand, WizardryItems.attunement_upgrade)];
|
||||
|
||||
for(int i=0; i<newSpells.length; i++){
|
||||
// Prevents both NPEs and AIOOBEs
|
||||
newSpells[i] = i < spells.length && spells[i] != null ? spells[i] : Spells.none;
|
||||
}
|
||||
|
||||
WandHelper.setSpells(wand, newSpells);
|
||||
|
||||
int[] cooldown = WandHelper.getCooldowns(wand);
|
||||
int[] newCooldown = new int[5 + WandHelper.getUpgradeLevel(wand, WizardryItems.attunement_upgrade)];
|
||||
|
||||
if(cooldown.length > 0){
|
||||
for(int i=0; i<cooldown.length; i++){
|
||||
newCooldown[i] = cooldown[i];
|
||||
}
|
||||
}
|
||||
|
||||
WandHelper.setCooldowns(wand, newCooldown);
|
||||
}
|
||||
|
||||
this.decrStackSize(ContainerArcaneWorkbench.UPGRADE_SLOT, 1);
|
||||
player.addStat(WizardryAchievements.special_upgrade, 1);
|
||||
|
||||
if(WandHelper.getTotalUpgrades(wand) == Tier.MASTER.upgradeLimit){
|
||||
player.addStat(WizardryAchievements.max_out_wand, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Reads NBT spell id array to variable, edits this, then writes it back to NBT.
|
||||
// Original spells are preserved; if a slot is left empty the existing spell binding will remain.
|
||||
// Accounts for spells which cannot be applied because they are above the wand's tier; these spells
|
||||
// will not bind but the existing spell in that slot will remain and other applicable spells will
|
||||
// be bound as normal, along with any upgrades and crystals.
|
||||
Spell[] spells = WandHelper.getSpells(wand);
|
||||
if(spells.length <= 0){
|
||||
// 5 here because if the spell array doesn't exist, the wand can't possibly have attunement upgrades
|
||||
spells = new Spell[5];
|
||||
}
|
||||
for(int i=0; i<spells.length; i++){
|
||||
if(spellBooks[i] != null && !(Spell.get(spellBooks[i].getItemDamage()).tier.level > ((ItemWand)wand.getItem()).tier.level)){
|
||||
spells[i] = Spell.get(spellBooks[i].getItemDamage());
|
||||
}
|
||||
}
|
||||
WandHelper.setSpells(wand, spells);
|
||||
|
||||
// Charges wand by appropriate amount
|
||||
if(crystals != null){
|
||||
int chargeDepleted = wand.getItemDamage();
|
||||
//System.out.println("Charge depleted: " + chargeDepleted);
|
||||
//System.out.println("Crystals found: " + crystals.stackSize);
|
||||
if(crystals.stackSize * Constants.MANA_PER_CRYSTAL < chargeDepleted){
|
||||
//System.out.println("charging");
|
||||
wand.setItemDamage(chargeDepleted - crystals.stackSize * Constants.MANA_PER_CRYSTAL);
|
||||
this.decrStackSize(ContainerArcaneWorkbench.CRYSTAL_SLOT, crystals.stackSize);
|
||||
}else if(chargeDepleted != 0){
|
||||
//System.out.println((int)Math.ceil(((double)chargeDepleted)/50));
|
||||
this.decrStackSize(ContainerArcaneWorkbench.CRYSTAL_SLOT, (int)Math.ceil(((double)chargeDepleted)/Constants.MANA_PER_CRYSTAL));
|
||||
wand.setItemDamage(0);
|
||||
}
|
||||
}
|
||||
|
||||
// Armour
|
||||
}else if(wand != null && wand.getItem() instanceof ItemWizardArmour){
|
||||
// Applies legendary upgrade
|
||||
if(upgrade != null && upgrade.getItem() == WizardryItems.armour_upgrade){
|
||||
if(!wand.hasTagCompound()){
|
||||
wand.setTagCompound(new NBTTagCompound());
|
||||
}
|
||||
if(!wand.getTagCompound().hasKey("legendary")){
|
||||
wand.getTagCompound().setBoolean("legendary", true);
|
||||
this.setInventorySlotContents(ContainerArcaneWorkbench.UPGRADE_SLOT, null);
|
||||
player.addStat(WizardryAchievements.legendary);
|
||||
}
|
||||
}
|
||||
// Charges armour by appropriate amount
|
||||
if(crystals != null){
|
||||
int chargeDepleted = wand.getItemDamage();
|
||||
if(crystals.stackSize * Constants.MANA_PER_CRYSTAL < chargeDepleted){
|
||||
wand.setItemDamage(chargeDepleted - crystals.stackSize * Constants.MANA_PER_CRYSTAL);
|
||||
this.decrStackSize(ContainerArcaneWorkbench.CRYSTAL_SLOT, crystals.stackSize);
|
||||
}else if(chargeDepleted != 0){
|
||||
this.decrStackSize(ContainerArcaneWorkbench.CRYSTAL_SLOT, (int)Math.ceil(((double)chargeDepleted)/Constants.MANA_PER_CRYSTAL));
|
||||
wand.setItemDamage(0);
|
||||
}
|
||||
}
|
||||
|
||||
// Scrolls
|
||||
}else if(wand != null && wand.getItem() == WizardryItems.blank_scroll){
|
||||
// Spells can only be bound to scrolls if the player has already cast them (prevents casting of master spells without getting a master wand)
|
||||
// This restriction does not apply in creative mode
|
||||
if(spellBooks[0] != null && (player.capabilities.isCreativeMode || (WizardData.get(player) != null
|
||||
&& WizardData.get(player).hasSpellBeenDiscovered(Spell.get(spellBooks[0].getItemDamage()))))
|
||||
&& crystals != null && crystals.stackSize * Constants.MANA_PER_CRYSTAL > Spell.get(spellBooks[0].getItemDamage()).cost){
|
||||
|
||||
this.decrStackSize(ContainerArcaneWorkbench.CRYSTAL_SLOT, (int)Math.ceil(((double)Spell.get(spellBooks[0].getItemDamage()).cost)/Constants.MANA_PER_CRYSTAL));
|
||||
this.setInventorySlotContents(ContainerArcaneWorkbench.WAND_SLOT, new ItemStack(WizardryItems.scroll, 1, spellBooks[0].getItemDamage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user