Applied-Energistics-2-tiler.../src/main/java/appeng/hooks/AETrading.java

128 lines
3.8 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.hooks;
import java.util.Random;
import net.minecraft.entity.passive.EntityVillager;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.village.MerchantRecipe;
import net.minecraft.village.MerchantRecipeList;
2014-12-29 21:59:05 +01:00
import cpw.mods.fml.common.registry.VillagerRegistry.IVillageTradeHandler;
import com.google.common.base.Optional;
2014-12-29 21:59:05 +01:00
import appeng.api.AEApi;
import appeng.api.definitions.IItemDefinition;
import appeng.api.definitions.IMaterials;
2014-12-29 21:59:05 +01:00
public class AETrading implements IVillageTradeHandler
{
@Override
public void manipulateTradesForVillager( EntityVillager villager, MerchantRecipeList recipeList, Random random )
{
final IMaterials materials = AEApi.instance().definitions().materials();
this.addMerchant( recipeList, materials.silicon(), 1, random, 2 );
this.addMerchant( recipeList, materials.certusQuartzCrystal(), 2, random, 4 );
this.addMerchant( recipeList, materials.certusQuartzDust(), 1, random, 3 );
this.addTrade( recipeList, materials.certusQuartzDust(), materials.certusQuartzCrystal(), random, 2 );
}
private void addMerchant( MerchantRecipeList list, IItemDefinition item, int emera, Random rand, int greed )
{
for( ItemStack itemStack : item.maybeStack( 1 ).asSet() )
{
// Sell
ItemStack from = itemStack.copy();
ItemStack to = new ItemStack( Items.emerald );
int multiplier = ( Math.abs( rand.nextInt() ) % 6 );
final int emeraldCost = emera + ( Math.abs( rand.nextInt() ) % greed ) - multiplier;
int mood = rand.nextInt() % 2;
from.stackSize = multiplier + mood;
to.stackSize = multiplier * emeraldCost - mood;
if( to.stackSize < 0 )
{
from.stackSize -= to.stackSize;
to.stackSize -= to.stackSize;
}
this.addToList( list, from, to );
// Buy
ItemStack reverseTo = from.copy();
ItemStack reverseFrom = to.copy();
reverseFrom.stackSize *= rand.nextFloat() * 3.0f + 1.0f;
this.addToList( list, reverseFrom, reverseTo );
}
}
private void addTrade( MerchantRecipeList list, IItemDefinition inputDefinition, IItemDefinition outputDefinition, Random rand, int conversionVariance )
{
final Optional<ItemStack> maybeInputStack = inputDefinition.maybeStack( 1 );
final Optional<ItemStack> maybeOutputStack = outputDefinition.maybeStack( 1 );
if( maybeInputStack.isPresent() && maybeOutputStack.isPresent() )
{
// Sell
ItemStack inputStack = maybeInputStack.get().copy();
ItemStack outputStack = maybeOutputStack.get().copy();
inputStack.stackSize = 1 + ( Math.abs( rand.nextInt() ) % ( 1 + conversionVariance ) );
outputStack.stackSize = 1;
this.addToList( list, inputStack, outputStack );
}
}
private void addToList( MerchantRecipeList l, ItemStack a, ItemStack b )
{
if( a.stackSize < 1 )
2015-04-29 02:30:53 +02:00
{
a.stackSize = 1;
2015-04-29 02:30:53 +02:00
}
if( b.stackSize < 1 )
2015-04-29 02:30:53 +02:00
{
b.stackSize = 1;
2015-04-29 02:30:53 +02:00
}
if( a.stackSize > a.getMaxStackSize() )
2015-04-29 02:30:53 +02:00
{
a.stackSize = a.getMaxStackSize();
2015-04-29 02:30:53 +02:00
}
if( b.stackSize > b.getMaxStackSize() )
2015-04-29 02:30:53 +02:00
{
b.stackSize = b.getMaxStackSize();
2015-04-29 02:30:53 +02:00
}
l.add( new MerchantRecipe( a, b ) );
}
}