2016-07-20 10:42:41 +02:00
|
|
|
package de.ellpeck.actuallyadditions.mod.items.lens;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2016-07-20 10:42:41 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.internal.IAtomicReconstructor;
|
|
|
|
import de.ellpeck.actuallyadditions.api.lens.Lens;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
|
2016-11-16 20:31:16 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2016-07-20 10:42:41 +02:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.enchantment.Enchantment;
|
|
|
|
import net.minecraft.enchantment.EnchantmentData;
|
|
|
|
import net.minecraft.enchantment.EnchantmentHelper;
|
|
|
|
import net.minecraft.entity.item.EntityItem;
|
|
|
|
import net.minecraft.init.Items;
|
|
|
|
import net.minecraft.item.Item;
|
2017-06-17 00:48:49 +02:00
|
|
|
import net.minecraft.item.ItemEnchantedBook;
|
2016-07-20 10:42:41 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public class LensDisenchanting extends Lens {
|
2016-07-20 10:42:41 +02:00
|
|
|
|
2016-07-20 11:16:27 +02:00
|
|
|
public static final int ENERGY_USE = 250000;
|
2016-07-20 10:42:41 +02:00
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean invoke(IBlockState hitState, BlockPos hitBlock, IAtomicReconstructor tile) {
|
|
|
|
if (tile.getEnergy() >= ENERGY_USE) {
|
|
|
|
List<EntityItem> items = tile.getWorldObject().getEntitiesWithinAABB(EntityItem.class, new AxisAlignedBB(hitBlock.getX(), hitBlock.getY(), hitBlock.getZ(), hitBlock.getX() + 1, hitBlock.getY() + 1, hitBlock.getZ() + 1));
|
|
|
|
if (items != null && !items.isEmpty()) {
|
2016-07-20 10:42:41 +02:00
|
|
|
EntityItem book = null;
|
|
|
|
EntityItem toDisenchant = null;
|
2019-05-02 09:10:29 +02:00
|
|
|
for (EntityItem item : items) {
|
|
|
|
if (item != null && !item.isDead) {
|
2017-06-29 18:30:02 +02:00
|
|
|
ItemStack stack = item.getItem();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (StackUtil.isValid(stack) && stack.getCount() == 1) {
|
2016-07-20 10:42:41 +02:00
|
|
|
Item stackItem = stack.getItem();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (stackItem == Items.BOOK || stackItem == Items.ENCHANTED_BOOK) {
|
|
|
|
if (book == null) {
|
2016-07-20 10:42:41 +02:00
|
|
|
book = item;
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2016-07-20 10:42:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2016-07-20 10:42:41 +02:00
|
|
|
Map<Enchantment, Integer> enchants = EnchantmentHelper.getEnchantments(stack);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (enchants != null && !enchants.isEmpty()) {
|
|
|
|
if (toDisenchant == null) {
|
2016-07-20 10:42:41 +02:00
|
|
|
toDisenchant = item;
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2016-07-20 10:42:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (book != null && toDisenchant != null) {
|
2017-06-29 18:30:02 +02:00
|
|
|
ItemStack disenchantStack = toDisenchant.getItem();
|
|
|
|
ItemStack bookStack = book.getItem();
|
2016-07-20 10:42:41 +02:00
|
|
|
|
|
|
|
Map<Enchantment, Integer> enchants = EnchantmentHelper.getEnchantments(disenchantStack);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (enchants != null && !enchants.isEmpty()) {
|
2016-07-20 10:42:41 +02:00
|
|
|
Enchantment enchant = enchants.keySet().iterator().next();
|
|
|
|
int level = enchants.get(enchant);
|
|
|
|
|
|
|
|
ItemStack newDisenchantStack = disenchantStack.copy();
|
|
|
|
ItemStack newBookStack;
|
2019-05-02 09:10:29 +02:00
|
|
|
if (bookStack.getItem() == Items.BOOK) {
|
2016-07-20 10:42:41 +02:00
|
|
|
newBookStack = new ItemStack(Items.ENCHANTED_BOOK);
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2016-07-20 10:42:41 +02:00
|
|
|
newBookStack = bookStack.copy();
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemUtil.removeEnchantment(newDisenchantStack, enchant);
|
2017-06-17 00:48:49 +02:00
|
|
|
ItemEnchantedBook.addEnchantment(newBookStack, new EnchantmentData(enchant, level));
|
2016-07-20 10:42:41 +02:00
|
|
|
|
|
|
|
EntityItem disenchanted = new EntityItem(toDisenchant.getEntityWorld(), toDisenchant.posX, toDisenchant.posY, toDisenchant.posZ, newDisenchantStack);
|
|
|
|
EntityItem newBook = new EntityItem(book.getEntityWorld(), book.posX, book.posY, book.posZ, newBookStack);
|
|
|
|
toDisenchant.setDead();
|
|
|
|
book.setDead();
|
2016-11-26 21:32:27 +01:00
|
|
|
tile.getWorldObject().spawnEntity(newBook);
|
|
|
|
tile.getWorldObject().spawnEntity(disenchanted);
|
2016-07-20 10:42:41 +02:00
|
|
|
|
|
|
|
tile.extractEnergy(ENERGY_USE);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public float[] getColor() {
|
|
|
|
return new float[] { 234F / 255F, 173F / 255F, 255F / 255F };
|
2016-07-20 10:42:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public int getDistance() {
|
2016-07-20 10:42:41 +02:00
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public boolean canInvoke(IAtomicReconstructor tile, EnumFacing sideToShootTo, int energyUsePerShot) {
|
|
|
|
return tile.getEnergy() - energyUsePerShot >= ENERGY_USE;
|
2016-07-20 10:42:41 +02:00
|
|
|
}
|
|
|
|
}
|