2016-05-16 22:52:27 +02:00
|
|
|
/*
|
|
|
|
* This file ("MethodHandler.java") is part of the Actually Additions mod for Minecraft.
|
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
* under the Actually Additions License to be found at
|
|
|
|
* http://ellpeck.de/actaddlicense
|
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2016-05-16 22:54:42 +02:00
|
|
|
* © 2015-2016 Ellpeck
|
2016-05-16 22:52:27 +02:00
|
|
|
*/
|
|
|
|
|
2016-05-14 13:51:18 +02:00
|
|
|
package de.ellpeck.actuallyadditions.mod.misc;
|
2016-01-05 04:47:35 +01:00
|
|
|
|
2016-10-30 17:13:46 +01:00
|
|
|
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
|
2016-07-31 17:20:49 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.booklet.BookletPage;
|
2016-08-01 15:10:13 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.booklet.IBookletChapter;
|
|
|
|
import de.ellpeck.actuallyadditions.api.booklet.IBookletEntry;
|
2016-05-14 14:14:06 +02:00
|
|
|
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.recipe.CoffeeIngredient;
|
2016-05-14 14:14:06 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.recipe.LensConversionRecipe;
|
2016-08-01 15:10:13 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.booklet.chapter.BookletChapter;
|
2016-07-31 17:20:49 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.booklet.page.PageCrafting;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.booklet.page.PageFurnace;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.booklet.page.PagePicture;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.booklet.page.PageTextOnly;
|
2016-06-05 12:15:02 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.values.ConfigBoolValues;
|
2016-05-14 14:14:06 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.items.lens.LensRecipeHandler;
|
2016-10-30 17:13:46 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.recipe.CrusherRecipeRegistry;
|
2016-05-14 14:14:06 +02:00
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.entity.item.EntityItem;
|
|
|
|
import net.minecraft.item.ItemBlock;
|
2016-01-05 04:47:35 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2016-07-31 17:20:49 +02:00
|
|
|
import net.minecraft.item.crafting.IRecipe;
|
2016-01-05 04:47:35 +01:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2016-03-18 23:47:22 +01:00
|
|
|
import net.minecraft.potion.Potion;
|
2016-01-05 04:47:35 +01:00
|
|
|
import net.minecraft.potion.PotionEffect;
|
2016-10-19 18:38:57 +02:00
|
|
|
import net.minecraft.util.EnumParticleTypes;
|
2016-07-31 17:20:49 +02:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
2016-05-14 14:14:06 +02:00
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-10-19 18:38:57 +02:00
|
|
|
import net.minecraft.world.WorldServer;
|
2016-01-05 04:47:35 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2016-05-14 14:14:06 +02:00
|
|
|
import java.util.List;
|
2016-01-05 04:47:35 +01:00
|
|
|
|
2016-05-14 13:51:18 +02:00
|
|
|
public class MethodHandler implements IMethodHandler{
|
2016-01-05 04:47:35 +01:00
|
|
|
|
2016-05-14 13:51:18 +02:00
|
|
|
@Override
|
|
|
|
public boolean addEffectToStack(ItemStack stack, CoffeeIngredient ingredient){
|
2016-01-05 04:47:35 +01:00
|
|
|
boolean worked = false;
|
|
|
|
if(ingredient != null){
|
|
|
|
PotionEffect[] effects = ingredient.getEffects();
|
|
|
|
if(effects != null && effects.length > 0){
|
|
|
|
for(PotionEffect effect : effects){
|
2016-05-14 13:51:18 +02:00
|
|
|
PotionEffect effectHas = this.getSameEffectFromStack(stack, effect);
|
2016-01-05 04:47:35 +01:00
|
|
|
if(effectHas != null){
|
|
|
|
if(effectHas.getAmplifier() < ingredient.maxAmplifier-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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
public PotionEffect getSameEffectFromStack(ItemStack stack, PotionEffect effect){
|
|
|
|
PotionEffect[] effectsStack = this.getEffectsFromStack(stack);
|
2016-01-05 04:47:35 +01:00
|
|
|
if(effectsStack != null && effectsStack.length > 0){
|
|
|
|
for(PotionEffect effectStack : effectsStack){
|
2016-03-18 23:47:22 +01:00
|
|
|
if(effect.getPotion() == effectStack.getPotion()){
|
2016-01-05 04:47:35 +01:00
|
|
|
return effectStack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-05-14 13:51:18 +02:00
|
|
|
@Override
|
|
|
|
public void addEffectProperties(ItemStack stack, PotionEffect effect, boolean addDur, boolean addAmp){
|
|
|
|
PotionEffect[] effects = this.getEffectsFromStack(stack);
|
2016-01-05 04:47:35 +01:00
|
|
|
stack.setTagCompound(new NBTTagCompound());
|
|
|
|
for(int i = 0; i < effects.length; i++){
|
2016-03-18 23:47:22 +01:00
|
|
|
if(effects[i].getPotion() == effect.getPotion()){
|
|
|
|
effects[i] = new PotionEffect(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
|
|
|
|
public void addEffectToStack(ItemStack stack, PotionEffect effect){
|
2016-01-05 04:47:35 +01:00
|
|
|
NBTTagCompound tag = stack.getTagCompound();
|
|
|
|
if(tag == null){
|
|
|
|
tag = new NBTTagCompound();
|
|
|
|
}
|
|
|
|
|
|
|
|
int prevCounter = tag.getInteger("Counter");
|
|
|
|
NBTTagCompound compound = new NBTTagCompound();
|
2016-03-18 23:47:22 +01:00
|
|
|
compound.setInteger("ID", Potion.getIdFromPotion(effect.getPotion()));
|
2016-01-05 04:47:35 +01:00
|
|
|
compound.setInteger("Duration", effect.getDuration());
|
|
|
|
compound.setInteger("Amplifier", effect.getAmplifier());
|
|
|
|
|
|
|
|
int counter = prevCounter+1;
|
|
|
|
tag.setTag(counter+"", compound);
|
|
|
|
tag.setInteger("Counter", counter);
|
|
|
|
|
|
|
|
stack.setTagCompound(tag);
|
|
|
|
}
|
|
|
|
|
2016-05-14 13:51:18 +02:00
|
|
|
@Override
|
|
|
|
public PotionEffect[] getEffectsFromStack(ItemStack stack){
|
2016-01-05 04:47:35 +01:00
|
|
|
ArrayList<PotionEffect> effects = new ArrayList<PotionEffect>();
|
|
|
|
NBTTagCompound tag = stack.getTagCompound();
|
|
|
|
if(tag != null){
|
|
|
|
int counter = tag.getInteger("Counter");
|
|
|
|
while(counter > 0){
|
|
|
|
NBTTagCompound compound = (NBTTagCompound)tag.getTag(counter+"");
|
2016-03-18 23:47:22 +01:00
|
|
|
PotionEffect effect = new PotionEffect(Potion.getPotionById(compound.getInteger("ID")), compound.getInteger("Duration"), compound.getByte("Amplifier"));
|
|
|
|
effects.add(effect);
|
2016-01-05 04:47:35 +01:00
|
|
|
counter--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return effects.size() > 0 ? effects.toArray(new PotionEffect[effects.size()]) : null;
|
|
|
|
}
|
2016-05-14 14:14:06 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean invokeConversionLens(IBlockState hitState, BlockPos hitBlock, IAtomicReconstructor tile){
|
2016-07-04 20:15:41 +02:00
|
|
|
if(hitBlock != null && !hitState.getBlock().isAir(hitState, tile.getWorldObject(), hitBlock)){
|
2016-05-14 14:14:06 +02:00
|
|
|
int range = 2;
|
|
|
|
|
|
|
|
//Converting the Blocks
|
|
|
|
for(int reachX = -range; reachX < range+1; reachX++){
|
|
|
|
for(int reachZ = -range; reachZ < range+1; reachZ++){
|
|
|
|
for(int reachY = -range; reachY < range+1; reachY++){
|
|
|
|
BlockPos pos = new BlockPos(hitBlock.getX()+reachX, hitBlock.getY()+reachY, hitBlock.getZ()+reachZ);
|
2016-10-19 18:38:57 +02:00
|
|
|
if(!tile.getWorldObject().isAirBlock(pos)){
|
|
|
|
IBlockState state = tile.getWorldObject().getBlockState(pos);
|
|
|
|
List<LensConversionRecipe> recipes = LensRecipeHandler.getRecipesFor(new ItemStack(state.getBlock(), 1, state.getBlock().getMetaFromState(state)));
|
|
|
|
for(LensConversionRecipe recipe : recipes){
|
|
|
|
if(recipe != null && recipe.type == tile.getLens() && tile.getEnergy() >= recipe.energyUse){
|
2016-10-30 17:13:46 +01:00
|
|
|
ItemStack output = recipe.outputStack;
|
|
|
|
if(output != null){
|
|
|
|
if(!ConfigBoolValues.LESS_BLOCK_BREAKING_EFFECTS.isEnabled()){
|
|
|
|
tile.getWorldObject().playEvent(2001, pos, Block.getStateId(tile.getWorldObject().getBlockState(pos)));
|
2016-05-14 14:14:06 +02:00
|
|
|
}
|
2016-10-30 17:13:46 +01:00
|
|
|
|
|
|
|
if(output.getItem() instanceof ItemBlock){
|
|
|
|
tile.getWorldObject().setBlockState(pos, Block.getBlockFromItem(output.getItem()).getStateFromMeta(output.getItemDamage()), 2);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
EntityItem item = new EntityItem(tile.getWorldObject(), pos.getX()+0.5, pos.getY()+0.5, pos.getZ()+0.5, output.copy());
|
|
|
|
tile.getWorldObject().spawnEntityInWorld(item);
|
|
|
|
tile.getWorldObject().setBlockToAir(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
tile.extractEnergy(recipe.energyUse);
|
|
|
|
break;
|
2016-05-14 14:14:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-19 18:38:57 +02:00
|
|
|
else{
|
|
|
|
if(tile.getWorldObject() instanceof WorldServer){
|
|
|
|
((WorldServer)tile.getWorldObject()).spawnParticle(EnumParticleTypes.END_ROD, false, pos.getX()+0.5, pos.getY()+0.5, pos.getZ()+0.5, 1, 0, 0, 0, 0D);
|
|
|
|
}
|
|
|
|
}
|
2016-05-14 14:14:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Converting the Items
|
|
|
|
ArrayList<EntityItem> items = (ArrayList<EntityItem>)tile.getWorldObject().getEntitiesWithinAABB(EntityItem.class, new AxisAlignedBB(hitBlock.getX()-range, hitBlock.getY()-range, hitBlock.getZ()-range, hitBlock.getX()+range, hitBlock.getY()+range, hitBlock.getZ()+range));
|
|
|
|
for(EntityItem item : items){
|
|
|
|
ItemStack stack = item.getEntityItem();
|
|
|
|
if(!item.isDead && stack != null){
|
|
|
|
List<LensConversionRecipe> recipes = LensRecipeHandler.getRecipesFor(stack);
|
|
|
|
for(LensConversionRecipe recipe : recipes){
|
2016-11-02 21:15:28 +01:00
|
|
|
if(recipe != null && recipe.type == tile.getLens()){
|
|
|
|
int itemsPossible = Math.min(tile.getEnergy()/recipe.energyUse, stack.stackSize);
|
2016-05-14 14:14:06 +02:00
|
|
|
|
2016-11-02 21:15:28 +01:00
|
|
|
if(itemsPossible > 0){
|
|
|
|
item.setDead();
|
2016-05-14 14:14:06 +02:00
|
|
|
|
2016-11-03 10:01:25 +01:00
|
|
|
if(stack.stackSize-itemsPossible > 0){
|
2016-11-02 21:15:28 +01:00
|
|
|
ItemStack stackCopy = stack.copy();
|
|
|
|
stackCopy.stackSize-=itemsPossible;
|
2016-05-14 14:14:06 +02:00
|
|
|
|
2016-11-02 21:15:28 +01:00
|
|
|
EntityItem inputLeft = new EntityItem(tile.getWorldObject(), item.posX, item.posY, item.posZ, stackCopy);
|
|
|
|
tile.getWorldObject().spawnEntityInWorld(inputLeft);
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemStack outputCopy = recipe.outputStack.copy();
|
|
|
|
outputCopy.stackSize = itemsPossible;
|
|
|
|
|
|
|
|
EntityItem newItem = new EntityItem(tile.getWorldObject(), item.posX, item.posY, item.posZ, outputCopy);
|
|
|
|
tile.getWorldObject().spawnEntityInWorld(newItem);
|
|
|
|
|
|
|
|
tile.extractEnergy(recipe.energyUse*itemsPossible);
|
|
|
|
break;
|
|
|
|
}
|
2016-05-14 14:14:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-31 17:20:49 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public BookletPage generateTextPage(int id){
|
|
|
|
return new PageTextOnly(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BookletPage generatePicturePage(int id, ResourceLocation resLoc, int textStartY){
|
|
|
|
return new PagePicture(id, resLoc, textStartY);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BookletPage generateCraftingPage(int id, IRecipe... recipes){
|
|
|
|
return new PageCrafting(id, recipes);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BookletPage generateFurnacePage(int id, ItemStack input, ItemStack result){
|
|
|
|
return new PageFurnace(id, input, result);
|
|
|
|
}
|
2016-08-01 15:10:13 +02:00
|
|
|
|
|
|
|
@Override
|
2016-08-02 00:36:14 +02:00
|
|
|
public IBookletChapter generateBookletChapter(String identifier, IBookletEntry entry, ItemStack displayStack, BookletPage... pages){
|
|
|
|
return new BookletChapter(identifier, entry, displayStack, pages);
|
2016-08-01 15:10:13 +02:00
|
|
|
}
|
2016-10-30 17:13:46 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean addCrusherRecipes(List<ItemStack> inputs, List<ItemStack> outputOnes, int outputOneAmounts, List<ItemStack> outputTwos, int outputTwoAmounts, int outputTwoChance){
|
|
|
|
boolean hasWorkedOnce = false;
|
|
|
|
for(ItemStack input : inputs){
|
|
|
|
if(input != null && CrusherRecipeRegistry.getRecipeFromInput(input) == null){
|
|
|
|
for(ItemStack outputOne : outputOnes){
|
|
|
|
if(outputOne != null && !CrusherRecipeRegistry.hasBlacklistedOutput(outputOne)){
|
|
|
|
ItemStack outputOneCopy = outputOne.copy();
|
|
|
|
outputOneCopy.stackSize = outputOneAmounts;
|
|
|
|
|
|
|
|
if(outputTwos == null || outputTwos.isEmpty()){
|
|
|
|
ActuallyAdditionsAPI.addCrusherRecipe(input, outputOneCopy, null, 0);
|
|
|
|
hasWorkedOnce = true;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
for(ItemStack outputTwo : outputTwos){
|
|
|
|
if(outputTwo != null && !CrusherRecipeRegistry.hasBlacklistedOutput(outputTwo)){
|
|
|
|
ItemStack outputTwoCopy = outputTwo.copy();
|
|
|
|
outputTwoCopy.stackSize = outputTwoAmounts;
|
|
|
|
|
|
|
|
ActuallyAdditionsAPI.addCrusherRecipe(input, outputOneCopy, outputTwoCopy, outputTwoChance);
|
|
|
|
hasWorkedOnce = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasWorkedOnce;
|
|
|
|
}
|
2016-01-05 04:47:35 +01:00
|
|
|
}
|