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>.
|
|
|
|
*/
|
|
|
|
|
2014-03-02 09:35:11 +01:00
|
|
|
package appeng.hooks;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-03-02 09:35:11 +01:00
|
|
|
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
|
|
|
|
2014-03-02 09:35:11 +01:00
|
|
|
import cpw.mods.fml.common.registry.VillagerRegistry.IVillageTradeHandler;
|
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
import com.google.common.base.Optional;
|
|
|
|
|
2014-12-29 21:59:05 +01:00
|
|
|
import appeng.api.AEApi;
|
2015-01-03 02:53:14 +01:00
|
|
|
import appeng.api.definitions.IItemDefinition;
|
|
|
|
import appeng.api.definitions.IMaterials;
|
|
|
|
|
2014-12-29 21:59:05 +01:00
|
|
|
|
2014-03-02 09:35:11 +01:00
|
|
|
public class AETrading implements IVillageTradeHandler
|
|
|
|
{
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
@Override
|
|
|
|
public void manipulateTradesForVillager( EntityVillager villager, MerchantRecipeList recipeList, Random random )
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
final IMaterials materials = AEApi.instance().definitions().materials();
|
2014-03-02 09:35:11 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
this.addMerchant( recipeList, materials.silicon(), 1, random, 2 );
|
|
|
|
this.addMerchant( recipeList, materials.certusQuartzCrystal(), 2, random, 4 );
|
|
|
|
this.addMerchant( recipeList, materials.certusQuartzDust(), 1, random, 3 );
|
2014-03-02 09:35:11 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
this.addTrade( recipeList, materials.certusQuartzDust(), materials.certusQuartzCrystal(), random, 2 );
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
private void addMerchant( MerchantRecipeList list, IItemDefinition item, int emera, Random rand, int greed )
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
for( ItemStack itemStack : item.maybeStack( 1 ).asSet() )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
|
|
|
// Sell
|
|
|
|
ItemStack from = itemStack.copy();
|
|
|
|
ItemStack to = new ItemStack( Items.emerald );
|
2014-03-02 09:35:11 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
int multiplier = ( Math.abs( rand.nextInt() ) % 6 );
|
|
|
|
final int emeraldCost = emera + ( Math.abs( rand.nextInt() ) % greed ) - multiplier;
|
2015-01-03 02:53:14 +01:00
|
|
|
int mood = rand.nextInt() % 2;
|
2014-03-02 09:35:11 +01:00
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
from.stackSize = multiplier + mood;
|
|
|
|
to.stackSize = multiplier * emeraldCost - mood;
|
2014-03-02 09:35:11 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( to.stackSize < 0 )
|
2015-01-03 02:53:14 +01:00
|
|
|
{
|
|
|
|
from.stackSize -= to.stackSize;
|
|
|
|
to.stackSize -= to.stackSize;
|
|
|
|
}
|
2014-03-02 09:35:11 +01:00
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
this.addToList( list, from, to );
|
2014-03-02 09:35:11 +01:00
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
// Buy
|
|
|
|
ItemStack reverseTo = from.copy();
|
|
|
|
ItemStack reverseFrom = to.copy();
|
2014-03-02 09:35:11 +01:00
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
reverseFrom.stackSize *= rand.nextFloat() * 3.0f + 1.0f;
|
2014-03-02 09:35:11 +01:00
|
|
|
|
2015-01-03 02:53:14 +01:00
|
|
|
this.addToList( list, reverseFrom, reverseTo );
|
|
|
|
}
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
private void addTrade( MerchantRecipeList list, IItemDefinition inputDefinition, IItemDefinition outputDefinition, Random rand, int conversionVariance )
|
2014-03-02 09:35:11 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
final Optional<ItemStack> maybeInputStack = inputDefinition.maybeStack( 1 );
|
|
|
|
final Optional<ItemStack> maybeOutputStack = outputDefinition.maybeStack( 1 );
|
2015-01-03 02:53:14 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( maybeInputStack.isPresent() && maybeOutputStack.isPresent() )
|
|
|
|
{
|
|
|
|
// Sell
|
|
|
|
ItemStack inputStack = maybeInputStack.get().copy();
|
|
|
|
ItemStack outputStack = maybeOutputStack.get().copy();
|
2014-03-02 09:35:11 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
inputStack.stackSize = 1 + ( Math.abs( rand.nextInt() ) % ( 1 + conversionVariance ) );
|
|
|
|
outputStack.stackSize = 1;
|
|
|
|
|
|
|
|
this.addToList( list, inputStack, outputStack );
|
|
|
|
}
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
private void addToList( MerchantRecipeList l, ItemStack a, ItemStack b )
|
|
|
|
{
|
|
|
|
if( a.stackSize < 1 )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
a.stackSize = 1;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
if( b.stackSize < 1 )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
b.stackSize = 1;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
|
|
|
if( a.stackSize > a.getMaxStackSize() )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
a.stackSize = a.getMaxStackSize();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
if( b.stackSize > b.getMaxStackSize() )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
b.stackSize = b.getMaxStackSize();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
|
|
|
l.add( new MerchantRecipe( a, b ) );
|
|
|
|
}
|
2014-03-02 09:35:11 +01:00
|
|
|
}
|