Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 108 additions & 110 deletions src/main/java/vazkii/botania/common/block/tile/TileEnchanter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
package vazkii.botania.common.block.tile;

import com.gtnewhorizon.gtnhlib.item.ItemStackNBT;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -183,17 +184,17 @@ public void onWanded(EntityPlayer player, ItemStack wand) {

@Override
public void updateEntity() {
if(getBlockMetadata() < PYLON_LOCATIONS.length)
for(int[] pylon : PYLON_LOCATIONS[getBlockMetadata()]) {
if (getBlockMetadata() < PYLON_LOCATIONS.length)
for (int[] pylon : PYLON_LOCATIONS[getBlockMetadata()]) {
TileEntity tile = worldObj.getTileEntity(xCoord + pylon[0], yCoord + pylon[1], zCoord + pylon[2]);
if(tile instanceof TilePylon)
((TilePylon) tile).activated = false;
if (tile instanceof TilePylon tilePylon)
tilePylon.activated = false;
}

if(!canEnchanterExist(worldObj, xCoord, yCoord, zCoord, getBlockMetadata())) {
if (!canEnchanterExist(worldObj, xCoord, yCoord, zCoord, getBlockMetadata())) {

worldObj.setBlock(xCoord, yCoord, zCoord, Blocks.lapis_block, 0, 1 | 2);
for(int i = 0; i < 50; i++) {
for (int i = 0; i < 50; i++) {
float red = (float) Math.random();
float green = (float) Math.random();
float blue = (float) Math.random();
Expand All @@ -202,104 +203,99 @@ public void updateEntity() {
worldObj.playSoundEffect(xCoord, yCoord, zCoord, "botania:enchanterBlock", 0.5F, 10F);
}

switch(stage) {
case 1 : { // Get books
if(stageTicks % 20 == 0) {
List<EntityItem> items = worldObj.getEntitiesWithinAABB(EntityItem.class, AxisAlignedBB.getBoundingBox(xCoord - 2, yCoord, zCoord - 2, xCoord + 3, yCoord + 1, zCoord + 3));
int count = items.size();
boolean addedEnch = false;

if(count > 0 && !worldObj.isRemote) {
for(EntityItem entity : items) {
ItemStack item = entity.getEntityItem();
if(item.getItem() == Items.enchanted_book) {
NBTTagList enchants = Items.enchanted_book.func_92110_g(item);
if(enchants != null && enchants.tagCount() > 0) {
NBTTagCompound enchant = enchants.getCompoundTagAt(0);
short enchantId = enchant.getShort("id");
short enchantLvl = enchant.getShort("lvl");
if(!hasEnchantAlready(enchantId) && isEnchantmentValid(enchantId)) {
this.enchants.add(new EnchantmentData(enchantId, enchantLvl));
worldObj.playSoundEffect(xCoord, yCoord, zCoord, "botania:ding", 1F, 1F);
addedEnch = true;
break;
}
}
}
}
}

if(!addedEnch) {
if(enchants.isEmpty())
stage = 0;
else advanceStage();
}
}
break;
}
case 2 : { // Get Mana
for(int[] pylon : PYLON_LOCATIONS[getBlockMetadata()]) {
TilePylon pylonTile = (TilePylon) worldObj.getTileEntity(xCoord + pylon[0], yCoord + pylon[1], zCoord + pylon[2]);
if(pylonTile != null) {
pylonTile.activated = true;
pylonTile.centerX = xCoord;
pylonTile.centerY = yCoord;
pylonTile.centerZ = zCoord;
}
}

if(manaRequired == -1) {
manaRequired = 0;
for(EnchantmentData data : enchants) {
Enchantment ench = Enchantment.enchantmentsList[data.enchant];
manaRequired += (int) (5000F * ((15 - Math.min(15, ench.getWeight())) * 1.05F) * ((3F + data.level * data.level) * 0.25F) * (0.9F + enchants.size() * 0.05F));
}
} else if(mana >= manaRequired) {
manaRequired = 0;
for(int[] pylon : PYLON_LOCATIONS[getBlockMetadata()])
((TilePylon) worldObj.getTileEntity(xCoord + pylon[0], yCoord + pylon[1], zCoord + pylon[2])).activated = false;

advanceStage();
} else {
ISparkEntity spark = getAttachedSpark();
if(spark != null) {
List<ISparkEntity> sparkEntities = SparkHelper.getSparksAround(worldObj, xCoord + 0.5, yCoord + 0.5, zCoord + 0.5);
for(ISparkEntity otherSpark : sparkEntities) {
if(spark == otherSpark)
continue;

if(otherSpark.getAttachedTile() != null && otherSpark.getAttachedTile() instanceof IManaPool)
otherSpark.registerTransfer(spark);
}
}
}

break;
}
case 3 : { // Enchant
if(stageTicks >= 100) {
for(EnchantmentData data : enchants)
if(EnchantmentHelper.getEnchantmentLevel(data.enchant, itemToEnchant) == 0)
itemToEnchant.addEnchantment(Enchantment.enchantmentsList[data.enchant], data.level);

enchants.clear();
manaRequired = -1;
mana = 0;

craftingFanciness();
advanceStage();
}
break;
}
case 4 : { // Reset
if(stageTicks >= 20)
advanceStage();

break;
}
}

if(stage != 0)
switch (stage) {
case 1 -> { // Get books
if (stageTicks % 20 == 0) {
List<EntityItem> items = worldObj.getEntitiesWithinAABB(EntityItem.class, AxisAlignedBB.getBoundingBox(xCoord - 2, yCoord, zCoord - 2, xCoord + 3, yCoord + 1, zCoord + 3));
int count = items.size();
boolean addedEnch = false;

if (count > 0 && !worldObj.isRemote) {
for (EntityItem entity : items) {
ItemStack item = entity.getEntityItem();
if (item.getItem() == Items.enchanted_book) {
NBTTagList enchants = Items.enchanted_book.func_92110_g(item);
if (enchants != null && enchants.tagCount() > 0) {
NBTTagCompound enchant = enchants.getCompoundTagAt(0);
short enchantId = enchant.getShort("id");
short enchantLvl = enchant.getShort("lvl");
if (!hasEnchantAlready(enchantId) && isEnchantmentValid(enchantId)) {
this.enchants.add(new EnchantmentData(enchantId, enchantLvl));
worldObj.playSoundEffect(xCoord, yCoord, zCoord, "botania:ding", 1F, 1F);
addedEnch = true;
break;
}
}
}
}
}

if (!addedEnch) {
if (enchants.isEmpty())
stage = 0;
else advanceStage();
}
}
}
case 2 -> { // Get Mana
for (int[] pylon : PYLON_LOCATIONS[getBlockMetadata()]) {
TilePylon pylonTile = (TilePylon) worldObj.getTileEntity(xCoord + pylon[0], yCoord + pylon[1], zCoord + pylon[2]);
if (pylonTile != null) {
pylonTile.activated = true;
pylonTile.centerX = xCoord;
pylonTile.centerY = yCoord;
pylonTile.centerZ = zCoord;
}
}

if (manaRequired == -1) {
manaRequired = 0;
for (EnchantmentData data : enchants) {
Enchantment ench = Enchantment.enchantmentsList[data.enchant];
manaRequired += (int) (5000F * ((15 - Math.min(15, ench.getWeight())) * 1.05F) * ((3F + data.level * data.level) * 0.25F) * (0.9F + enchants.size() * 0.05F));
}
} else if (mana >= manaRequired) {
manaRequired = 0;
for (int[] pylon : PYLON_LOCATIONS[getBlockMetadata()])
((TilePylon) worldObj.getTileEntity(xCoord + pylon[0], yCoord + pylon[1], zCoord + pylon[2])).activated = false;

advanceStage();
} else {
ISparkEntity spark = getAttachedSpark();
if (spark != null) {
List<ISparkEntity> sparkEntities = SparkHelper.getSparksAround(worldObj, xCoord + 0.5, yCoord + 0.5, zCoord + 0.5);
for (ISparkEntity otherSpark : sparkEntities) {
if (spark == otherSpark)
continue;

if (otherSpark.getAttachedTile() != null && otherSpark.getAttachedTile() instanceof IManaPool)
otherSpark.registerTransfer(spark);
}
}
}
}
case 3 -> { // Enchant
if (stageTicks >= 100) {
for (EnchantmentData data : enchants)
if (EnchantmentHelper.getEnchantmentLevel(data.enchant, itemToEnchant) == 0) {
ItemStackNBT.enchant(itemToEnchant, data.enchant, data.level);
}

enchants.clear();
manaRequired = -1;
mana = 0;

craftingFanciness();
advanceStage();
}
}
case 4 -> { // Reset
if (stageTicks >= 20)
advanceStage();
}
}

if (stage != 0)
stageTicks++;
}

Expand Down Expand Up @@ -388,12 +384,14 @@ public void readCustomNBT(NBTTagCompound cmp) {

enchants.clear();
String enchStr = cmp.getString(TAG_ENCHANTS);
if(!enchStr.isEmpty()) {
if (!enchStr.isEmpty()) {
String[] enchTokens = enchStr.split(",");
for(String token : enchTokens) {
for (String token : enchTokens) {
String[] entryTokens = token.split(":");
int id = Integer.parseInt(entryTokens[0]);
int lvl = Integer.parseInt(entryTokens[1]);
short id = Short.parseShort(entryTokens[0]);
short lvl = Short.parseShort(entryTokens[1]);

// Normally, I'd prefer writing these a
enchants.add(new EnchantmentData(id, lvl));
}
}
Expand Down Expand Up @@ -479,9 +477,9 @@ public void renderHUD(Minecraft mc, ScaledResolution res) {

private static class EnchantmentData {

public int enchant, level;
public short enchant, level;

public EnchantmentData(int enchant, int level) {
public EnchantmentData(short enchant, short level) {
this.enchant = enchant;
this.level = level;
}
Expand Down