ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/common/misc/apiimpl/MethodHandler.java

353 lines
17 KiB
Java
Raw Normal View History

2020-09-09 16:49:01 +02:00
package de.ellpeck.actuallyadditions.common.misc.apiimpl;
2016-01-05 04:47:35 +01:00
import java.util.ArrayList;
import java.util.List;
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
2016-11-12 15:13:20 +01:00
import de.ellpeck.actuallyadditions.api.booklet.IBookletChapter;
import de.ellpeck.actuallyadditions.api.booklet.IBookletEntry;
import de.ellpeck.actuallyadditions.api.booklet.IBookletPage;
import de.ellpeck.actuallyadditions.api.internal.IAtomicReconstructor;
2016-05-14 13:51:18 +02:00
import de.ellpeck.actuallyadditions.api.internal.IMethodHandler;
import de.ellpeck.actuallyadditions.api.lens.Lens;
import de.ellpeck.actuallyadditions.api.recipe.LensConversionRecipe;
2020-09-09 16:49:01 +02:00
import de.ellpeck.actuallyadditions.common.blocks.BlockLaserRelay;
import de.ellpeck.actuallyadditions.booklet.chapter.BookletChapter;
import de.ellpeck.actuallyadditions.booklet.chapter.BookletChapterTrials;
import de.ellpeck.actuallyadditions.booklet.page.PageCrafting;
import de.ellpeck.actuallyadditions.booklet.page.PageFurnace;
import de.ellpeck.actuallyadditions.booklet.page.PagePicture;
import de.ellpeck.actuallyadditions.booklet.page.PageTextOnly;
2020-09-09 16:49:01 +02:00
import de.ellpeck.actuallyadditions.common.config.values.ConfigStringListValues;
import de.ellpeck.actuallyadditions.common.items.lens.LensRecipeHandler;
import de.ellpeck.actuallyadditions.common.recipe.CrusherRecipeRegistry;
import de.ellpeck.actuallyadditions.common.tile.TileEntityAtomicReconstructor;
import de.ellpeck.actuallyadditions.common.util.StackUtil;
import net.minecraft.block.Block;
2020-09-21 19:59:08 +02:00
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.item.ItemEntity;
2016-01-05 04:47:35 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
2020-09-21 19:59:08 +02:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.potion.EffectInstance;
2016-03-18 23:47:22 +01:00
import net.minecraft.potion.Potion;
2020-09-21 19:59:08 +02:00
import net.minecraft.util.Direction;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
2019-05-18 20:16:27 +02:00
import net.minecraft.util.math.Vec3i;
import net.minecraftforge.common.util.FakePlayerFactory;
2016-01-05 04:47:35 +01:00
2019-05-02 09:10:29 +02:00
public class MethodHandler implements IMethodHandler {
2016-01-05 04:47:35 +01:00
2016-05-14 13:51:18 +02:00
@Override
2019-05-02 09:10:29 +02:00
public boolean addEffectToStack(ItemStack stack, CoffeeIngredient ingredient) {
2016-01-05 04:47:35 +01:00
boolean worked = false;
2019-05-02 09:10:29 +02:00
if (ingredient != null) {
2020-09-21 19:59:08 +02:00
EffectInstance[] effects = ingredient.getEffects();
2019-05-02 09:10:29 +02:00
if (effects != null && effects.length > 0) {
2020-09-21 19:59:08 +02:00
for (EffectInstance effect : effects) {
EffectInstance effectHas = this.getSameEffectFromStack(stack, effect);
2019-05-02 09:10:29 +02:00
if (effectHas != null) {
if (effectHas.getAmplifier() < ingredient.getMaxAmplifier() - 1) {
2016-05-14 13:51:18 +02:00
this.addEffectProperties(stack, effect, false, true);
2016-01-05 04:47:35 +01:00
worked = true;
}
2019-05-02 09:10:29 +02:00
} else {
2016-05-14 13:51:18 +02:00
this.addEffectToStack(stack, effect);
2016-01-05 04:47:35 +01:00
worked = true;
}
}
}
}
return worked;
}
2016-05-14 13:51:18 +02:00
@Override
2020-09-21 19:59:08 +02:00
public EffectInstance getSameEffectFromStack(ItemStack stack, EffectInstance effect) {
EffectInstance[] effectsStack = this.getEffectsFromStack(stack);
2019-05-02 09:10:29 +02:00
if (effectsStack != null && effectsStack.length > 0) {
2020-09-21 19:59:08 +02:00
for (EffectInstance effectStack : effectsStack) {
2019-05-02 09:10:29 +02:00
if (effect.getPotion() == effectStack.getPotion()) { return effectStack; }
2016-01-05 04:47:35 +01:00
}
}
return null;
}
2016-05-14 13:51:18 +02:00
@Override
2020-09-21 19:59:08 +02:00
public void addEffectProperties(ItemStack stack, EffectInstance effect, boolean addDur, boolean addAmp) {
EffectInstance[] effects = this.getEffectsFromStack(stack);
stack.setTag(new CompoundNBT());
2019-05-02 09:10:29 +02:00
for (int i = 0; i < effects.length; i++) {
if (effects[i].getPotion() == effect.getPotion()) {
2020-09-21 19:59:08 +02:00
effects[i] = new EffectInstance(effects[i].getPotion(), effects[i].getDuration() + (addDur ? effect.getDuration() : 0), effects[i].getAmplifier() + (addAmp ? effect.getAmplifier() > 0 ? effect.getAmplifier() : 1 : 0));
2016-01-05 04:47:35 +01:00
}
2016-05-14 13:51:18 +02:00
this.addEffectToStack(stack, effects[i]);
2016-01-05 04:47:35 +01:00
}
}
2016-05-14 13:51:18 +02:00
@Override
2020-09-21 19:59:08 +02:00
public void addEffectToStack(ItemStack stack, EffectInstance effect) {
CompoundNBT tag = stack.getOrCreateTag();
2016-01-05 04:47:35 +01:00
2020-09-21 19:59:08 +02:00
int prevCounter = tag.getInt("Counter");
CompoundNBT compound = new CompoundNBT();
compound.putInt("ID", Potion.getIdFromPotion(effect.getPotion()));
compound.putInt("Duration", effect.getDuration());
compound.putInt("Amplifier", effect.getAmplifier());
2016-01-05 04:47:35 +01:00
2019-05-02 09:10:29 +02:00
int counter = prevCounter + 1;
2020-09-21 19:59:08 +02:00
tag.put(counter + "", compound);
tag.putInt("Counter", counter);
2016-01-05 04:47:35 +01:00
2020-09-21 19:59:08 +02:00
stack.setTag(tag);
2016-01-05 04:47:35 +01:00
}
2016-05-14 13:51:18 +02:00
@Override
2020-09-21 19:59:08 +02:00
public EffectInstance[] getEffectsFromStack(ItemStack stack) {
ArrayList<EffectInstance> effects = new ArrayList<>();
CompoundNBT tag = stack.getOrCreateTag();
int counter = tag.getInt("Counter");
while (counter > 0) {
CompoundNBT compound = (CompoundNBT) tag.get(counter + "");
EffectInstance effect = new EffectInstance(Potion.getPotionById(compound.getInt("ID")), compound.getInt("Duration"), compound.getByte("Amplifier"));
effects.add(effect);
counter--;
2016-01-05 04:47:35 +01:00
}
2020-09-21 19:59:08 +02:00
return effects.size() > 0 ? effects.toArray(new EffectInstance[effects.size()]) : null;
2016-01-05 04:47:35 +01:00
}
@Override
2020-09-21 19:59:08 +02:00
public boolean invokeConversionLens(BlockState hitState, BlockPos hitBlock, IAtomicReconstructor tile) {
2019-05-02 09:10:29 +02:00
if (hitBlock != null) {
int range = 1;
int rangeX = 0;
int rangeY = 0;
int rangeZ = 0;
2020-09-21 19:59:08 +02:00
Direction facing = tile.getOrientation();
if (facing != Direction.UP && facing != Direction.DOWN) {
rangeY = range;
2020-09-21 19:59:08 +02:00
if (facing == Direction.NORTH || facing == Direction.SOUTH) {
rangeX = range;
2019-05-02 09:10:29 +02:00
} else {
rangeZ = range;
}
2019-05-02 09:10:29 +02:00
} else {
rangeX = range;
rangeZ = range;
}
//Converting the Blocks
2019-05-02 09:10:29 +02:00
for (int reachX = -rangeX; reachX <= rangeX; reachX++) {
for (int reachZ = -rangeZ; reachZ <= rangeZ; reachZ++) {
for (int reachY = -rangeY; reachY <= rangeY; reachY++) {
BlockPos pos = new BlockPos(hitBlock.getX() + reachX, hitBlock.getY() + reachY, hitBlock.getZ() + reachZ);
if (!tile.getWorldObject().isAirBlock(pos)) {
2020-09-21 19:59:08 +02:00
BlockState state = tile.getWorldObject().getBlockState(pos);
2019-05-02 09:10:29 +02:00
if (state.getBlock() instanceof BlockLaserRelay) continue;
LensConversionRecipe recipe = LensRecipeHandler.findMatchingRecipe(new ItemStack(state.getBlock(), 1, state.getBlock().getMetaFromState(state)), tile.getLens());
2019-05-02 09:10:29 +02:00
if (recipe != null && tile.getEnergy() >= recipe.getEnergyUsed()) {
2019-02-27 19:53:05 +01:00
ItemStack output = recipe.getOutput();
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(output)) {
2019-02-27 19:53:05 +01:00
tile.getWorldObject().playEvent(2001, pos, Block.getStateId(state));
recipe.transformHook(ItemStack.EMPTY, state, pos, tile);
2019-05-02 09:10:29 +02:00
if (output.getItem() instanceof ItemBlock) {
2019-02-27 19:53:05 +01:00
Block toPlace = Block.getBlockFromItem(output.getItem());
2020-09-21 19:59:08 +02:00
BlockState state2Place = toPlace.getStateForPlacement(tile.getWorldObject(), pos, facing, 0, 0, 0, output.getMetadata(), FakePlayerFactory.getMinecraft((WorldServer) tile.getWorldObject()), EnumHand.MAIN_HAND);
tile.getWorld().setBlockState(pos, state2Place, 2);
2019-05-02 09:10:29 +02:00
} else {
2020-09-21 19:59:08 +02:00
ItemEntity item = new ItemEntity(tile.getWorldObject(), pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, output.copy());
tile.getWorld().addEntity(item);
tile.getWorld().setBlockState(pos, Blocks.AIR.getDefaultState());
2019-02-27 19:53:05 +01:00
}
tile.extractEnergy(recipe.getEnergyUsed());
break;
}
}
}
}
2019-02-27 19:53:05 +01:00
}
}
//Converting the Items
2019-05-02 10:17:02 +02:00
AxisAlignedBB aabb = new AxisAlignedBB(tile.getPosition().getX(), tile.getPosition().getY(), tile.getPosition().getZ(), hitBlock.getX() + 1, hitBlock.getY() + 1, hitBlock.getZ() + 1);
2019-05-18 20:16:27 +02:00
Vec3i dir = tile.getOrientation().getDirectionVec();
aabb = aabb.grow(0.02, 0.02, 0.02).expand(dir.getX(), dir.getY(), dir.getZ());
2020-09-21 19:59:08 +02:00
List<ItemEntity> items = tile.getWorldObject().getEntitiesWithinAABB(ItemEntity.class, aabb);
for (ItemEntity item : items) {
ItemStack stack = item.getItem();
2020-09-21 19:59:08 +02:00
if (item.isAlive() && StackUtil.isValid(stack) && !item.getEntityData().getBoolean("aa_cnv")) {
LensConversionRecipe recipe = LensRecipeHandler.findMatchingRecipe(stack, tile.getLens());
2019-05-02 09:10:29 +02:00
if (recipe != null) {
int itemsPossible = Math.min(tile.getEnergy() / recipe.getEnergyUsed(), stack.getCount());
2019-05-02 09:10:29 +02:00
if (itemsPossible > 0) {
2019-02-27 19:53:05 +01:00
recipe.transformHook(item.getItem(), null, item.getPosition(), tile);
2020-09-21 19:59:08 +02:00
item.remove();
2019-05-02 09:10:29 +02:00
if (stack.getCount() - itemsPossible > 0) {
2019-02-27 19:53:05 +01:00
ItemStack stackCopy = stack.copy();
stackCopy.shrink(itemsPossible);
2020-09-21 19:59:08 +02:00
ItemEntity inputLeft = new ItemEntity(tile.getWorldObject(), item.posX, item.posY, item.posZ, stackCopy);
tile.getWorld().addEntity(inputLeft);
2019-02-27 19:53:05 +01:00
}
2019-02-27 19:53:05 +01:00
ItemStack outputCopy = recipe.getOutput().copy();
outputCopy.setCount(itemsPossible);
2020-09-21 19:59:08 +02:00
ItemEntity newItem = new ItemEntity(tile.getWorldObject(), item.posX, item.posY, item.posZ, outputCopy);
2019-07-09 07:38:24 +02:00
newItem.getEntityData().setBoolean("aa_cnv", true);
2019-02-27 19:53:05 +01:00
tile.getWorldObject().spawnEntity(newItem);
2019-05-02 09:10:29 +02:00
tile.extractEnergy(recipe.getEnergyUsed() * itemsPossible);
2019-02-27 19:53:05 +01:00
break;
}
}
2019-02-27 19:53:05 +01:00
}
}
return !hitState.getBlock().isAir(hitState, tile.getWorldObject(), hitBlock);
}
return false;
}
@Override
2019-05-02 09:10:29 +02:00
public boolean invokeReconstructor(IAtomicReconstructor tile) {
if (tile.getEnergy() >= TileEntityAtomicReconstructor.ENERGY_USE) {
2020-09-21 19:59:08 +02:00
Direction sideToManipulate = tile.getOrientation();
Lens currentLens = tile.getLens();
2019-05-02 09:10:29 +02:00
if (currentLens.canInvoke(tile, sideToManipulate, TileEntityAtomicReconstructor.ENERGY_USE)) {
tile.extractEnergy(TileEntityAtomicReconstructor.ENERGY_USE);
int distance = currentLens.getDistance();
2019-05-02 09:10:29 +02:00
for (int i = 0; i < distance; i++) {
BlockPos hitBlock = tile.getPosition().offset(sideToManipulate, i + 1);
2019-05-02 09:10:29 +02:00
if (currentLens.invoke(tile.getWorldObject().getBlockState(hitBlock), hitBlock, tile) || i >= distance - 1) {
TileEntityAtomicReconstructor.shootLaser(tile.getWorldObject(), tile.getX(), tile.getY(), tile.getZ(), hitBlock.getX(), hitBlock.getY(), hitBlock.getZ(), currentLens);
break;
}
}
return true;
}
}
return false;
}
@Override
2019-05-02 09:10:29 +02:00
public boolean addCrusherRecipes(List<ItemStack> inputs, List<ItemStack> outputOnes, int outputOneAmounts, List<ItemStack> outputTwos, int outputTwoAmounts, int outputTwoChance) {
boolean hasWorkedOnce = false;
2019-05-02 09:10:29 +02:00
for (ItemStack input : inputs) {
if (StackUtil.isValid(input) && CrusherRecipeRegistry.getRecipeFromInput(input) == null) {
for (ItemStack outputOne : outputOnes) {
if (StackUtil.isValid(outputOne) && !CrusherRecipeRegistry.hasBlacklistedOutput(outputOne, ConfigStringListValues.CRUSHER_OUTPUT_BLACKLIST.getValue())) {
ItemStack outputOneCopy = outputOne.copy();
outputOneCopy.setCount(outputOneAmounts);
2019-05-02 09:10:29 +02:00
if (outputTwos.isEmpty()) {
2017-11-02 22:49:53 +01:00
ActuallyAdditionsAPI.addCrusherRecipe(input, outputOneCopy, StackUtil.getEmpty(), 0);
hasWorkedOnce = true;
2019-05-02 09:10:29 +02:00
} else {
for (ItemStack outputTwo : outputTwos) {
if (StackUtil.isValid(outputTwo) && !CrusherRecipeRegistry.hasBlacklistedOutput(outputTwo, ConfigStringListValues.CRUSHER_OUTPUT_BLACKLIST.getValue())) {
ItemStack outputTwoCopy = outputTwo.copy();
outputTwoCopy.setCount(outputTwoAmounts);
ActuallyAdditionsAPI.addCrusherRecipe(input, outputOneCopy, outputTwoCopy, outputTwoChance);
hasWorkedOnce = true;
}
}
}
}
}
}
}
return hasWorkedOnce;
}
2019-02-27 19:53:05 +01:00
@Override
2019-05-02 09:10:29 +02:00
public boolean addCrusherRecipes(List<ItemStack> inputs, ItemStack outputOne, int outputOneAmount, ItemStack outputTwo, int outputTwoAmount, int outputTwoChance) {
boolean hasWorkedOnce = false;
2019-05-02 09:10:29 +02:00
for (ItemStack input : inputs) {
if (StackUtil.isValid(input) && CrusherRecipeRegistry.getRecipeFromInput(input) == null) {
if (StackUtil.isValid(outputOne) && !CrusherRecipeRegistry.hasBlacklistedOutput(outputOne, ConfigStringListValues.CRUSHER_OUTPUT_BLACKLIST.getValue())) {
2019-02-27 19:53:05 +01:00
ItemStack outputOneCopy = outputOne.copy();
outputOneCopy.setCount(outputOneAmount);
2019-05-02 09:10:29 +02:00
if (!StackUtil.isValid(outputTwo)) {
2019-02-27 19:53:05 +01:00
ActuallyAdditionsAPI.addCrusherRecipe(input, outputOneCopy, StackUtil.getEmpty(), 0);
hasWorkedOnce = true;
2019-05-02 09:10:29 +02:00
} else if (StackUtil.isValid(outputTwo) && !CrusherRecipeRegistry.hasBlacklistedOutput(outputTwo, ConfigStringListValues.CRUSHER_OUTPUT_BLACKLIST.getValue())) {
2019-02-27 19:53:05 +01:00
ItemStack outputTwoCopy = outputTwo.copy();
outputTwoCopy.setCount(outputTwoAmount);
2019-02-27 19:53:05 +01:00
ActuallyAdditionsAPI.addCrusherRecipe(input, outputOneCopy, outputTwoCopy, outputTwoChance);
hasWorkedOnce = true;
}
}
2019-02-27 19:53:05 +01:00
}
}
return hasWorkedOnce;
}
2016-11-12 15:13:20 +01:00
@Override
2019-05-02 09:10:29 +02:00
public IBookletPage generateTextPage(int id) {
2016-11-22 22:54:35 +01:00
return this.generateTextPage(id, 0);
2016-11-12 15:13:20 +01:00
}
@Override
2019-05-02 09:10:29 +02:00
public IBookletPage generatePicturePage(int id, ResourceLocation resLoc, int textStartY) {
2016-11-22 22:54:35 +01:00
return this.generatePicturePage(id, resLoc, textStartY, 0);
2016-11-12 15:13:20 +01:00
}
@Override
2019-05-02 09:10:29 +02:00
public IBookletPage generateCraftingPage(int id, IRecipe... recipes) {
2016-11-22 22:54:35 +01:00
return this.generateCraftingPage(id, 0, recipes);
2016-11-12 15:13:20 +01:00
}
@Override
2019-05-02 09:10:29 +02:00
public IBookletPage generateFurnacePage(int id, ItemStack input, ItemStack result) {
2016-11-22 22:54:35 +01:00
return this.generateFurnacePage(id, input, result, 0);
2016-11-12 15:13:20 +01:00
}
@Override
2019-05-02 09:10:29 +02:00
public IBookletChapter generateBookletChapter(String identifier, IBookletEntry entry, ItemStack displayStack, IBookletPage... pages) {
2016-11-22 22:54:35 +01:00
return this.generateBookletChapter(identifier, entry, displayStack, 0, pages);
}
@Override
2019-05-02 09:10:29 +02:00
public IBookletPage generateTextPage(int id, int priority) {
2016-11-22 22:54:35 +01:00
return new PageTextOnly(id, priority);
}
@Override
2019-05-02 09:10:29 +02:00
public IBookletPage generatePicturePage(int id, ResourceLocation resLoc, int textStartY, int priority) {
2016-11-22 22:54:35 +01:00
return new PagePicture(id, resLoc, textStartY, priority);
}
@Override
2019-05-02 09:10:29 +02:00
public IBookletPage generateCraftingPage(int id, int priority, IRecipe... recipes) {
2016-11-22 22:54:35 +01:00
return new PageCrafting(id, priority, recipes);
}
@Override
2019-05-02 09:10:29 +02:00
public IBookletPage generateFurnacePage(int id, ItemStack input, ItemStack result, int priority) {
2016-11-22 22:54:35 +01:00
return new PageFurnace(id, result, priority);
}
@Override
2019-05-02 09:10:29 +02:00
public IBookletChapter generateBookletChapter(String identifier, IBookletEntry entry, ItemStack displayStack, int priority, IBookletPage... pages) {
2016-11-22 22:54:35 +01:00
return new BookletChapter(identifier, entry, displayStack, priority, pages);
2016-11-12 15:13:20 +01:00
}
2017-02-18 00:54:58 +01:00
@Override
2019-05-02 09:10:29 +02:00
public IBookletChapter createTrial(String identifier, ItemStack displayStack, boolean textOnSecondPage) {
2017-02-18 00:54:58 +01:00
return new BookletChapterTrials(identifier, displayStack, textOnSecondPage);
}
2016-01-05 04:47:35 +01:00
}