aboutsummaryrefslogtreecommitdiff
path: root/src/Java/gtPlusPlus/xmod/forestry/trees/ForestryLeaf.java
blob: e2550f464397d6be9c805142e773811112d3ae18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package gtPlusPlus.xmod.forestry.trees;

import java.util.*;

import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import powercrystals.minefactoryreloaded.api.*;
import powercrystals.minefactoryreloaded.farmables.harvestables.HarvestableTreeLeaves;
import forestry.api.arboriculture.*;
import forestry.api.genetics.*;


public class ForestryLeaf extends HarvestableTreeLeaves implements IFactoryFruit
{
	private ITreeRoot root;
	private ReplacementBlock repl;
	protected Item _item;

	public ForestryLeaf(Block block)
	{
		super(block);
		root = (ITreeRoot)AlleleManager.alleleRegistry.getSpeciesRoot("rootTrees");
		repl = EmptyReplacement.INSTANCE;
		_item = Item.getItemFromBlock(block);
	}

	@Override
	public boolean canBePicked(World world, int x, int y, int z)
	{
		TileEntity te = world.getTileEntity(x, y, z);
		if (te instanceof IFruitBearer)
		{
			IFruitBearer fruit = (IFruitBearer)te;
			return fruit.getRipeness() >= 0.99f;
		}
		return false;
	}

	public boolean canFertilize(World world, int x, int y, int z)
	{
		return !canBePicked(world, x, y, z);
	}

	public boolean fertilize(World world, Random rand, int x, int y, int z)
	{
		TileEntity te = world.getTileEntity(x, y, z);
		if (te instanceof IFruitBearer)
		{
			IFruitBearer fruit = (IFruitBearer)te;
			fruit.addRipeness(1f);
			return true;
		}
		return false;
	}

	@Override
	public ReplacementBlock getReplacementBlock(World world, int x, int y, int z)
	{
		return repl;
	}

	@Override
	public void prePick(World world, int x, int y, int z)
	{
	}

	@Override // HARVESTER
	public List<ItemStack> getDrops(World world, Random rand, Map<String, Boolean> settings, int x, int y, int z)
	{
		ITree tree = getTree(world, x, y, z);
		if (tree == null)
			return null;

		ArrayList<ItemStack> prod = new ArrayList<ItemStack>();

		float modifier = 1f;
		if (settings.get("silkTouch") == Boolean.TRUE)
		{
			ItemStack item = new ItemStack(_item);
			NBTTagCompound tag = new NBTTagCompound();
			tree.writeToNBT(tag);
			item.setTagCompound(tag);
			prod.add(item);
		}
		else
		{
			boolean hasMate = tree.getMate() != null;
			for (ITree s : getSaplings(tree, world, x, y, z, modifier))
				if (s != null) {
					if ((hasMate && !s.isGeneticEqual(tree)) || rand.nextInt(32) == 0)
						if (rand.nextBoolean())
							prod.add(root.getMemberStack(s, EnumGermlingType.POLLEN.ordinal()));

					prod.add(root.getMemberStack(s, EnumGermlingType.SAPLING.ordinal()));
				}

			getFruits(world, x, y, z, tree, prod);
		}

		return prod;
	}

	
	private static ITree[] getSaplings(ITree tree, World world, int x, int y, int z, float modifier) {
		return tree.getSaplings(world, null, x, y, z, modifier);
	}

	@Override // FRUIT PICKER
	public List<ItemStack> getDrops(World world, Random rand, int x, int y, int z)
	{
		ITree tree = getTree(world, x, y, z);
		if (tree == null)
			return null;

		ArrayList<ItemStack> prod = new ArrayList<ItemStack>();
		getFruits(world, x, y, z, tree, prod);
		return prod;
	}

	private ITree getTree(World world, int x, int y, int z)
	{
		TileEntity te = world.getTileEntity(x, y, z);
		if (te instanceof IPollinatable) {
			IIndividual t = ((IPollinatable)te).getPollen();
			if (t instanceof ITree)
				return (ITree)t;
		}
		return null;
	}

	private void getFruits(World world, int x, int y, int z, ITree tree, ArrayList<ItemStack> prod)
	{
		TileEntity te = world.getTileEntity(x, y, z);
		if (te instanceof IFruitBearer)
		{
			IFruitBearer fruit = (IFruitBearer)te;
			if (fruit.hasFruit())
			{
				//int period = tree.getGenome().getFruitProvider().getRipeningPeriod();
				//ItemStack[] o = tree.produceStacks(world, x, y, z, (int)(fruit.getRipeness() * period + 0.1f));
				prod.addAll(fruit.pickFruit(null));
			}
		}
	}

	@Override
	public void postPick(World world, int x, int y, int z)
	{
		TileEntity te = world.getTileEntity(x, y, z);
		if (te instanceof IFruitBearer)
		{
			IFruitBearer fruit = (IFruitBearer)te;
			fruit.addRipeness(-fruit.getRipeness());
		}
	}
}