forked from iguanaman/HungerOverhaul
/
ModulePlantGrowth.java
108 lines (92 loc) · 4.05 KB
/
ModulePlantGrowth.java
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
package iguanaman.hungeroverhaul.module;
import iguanaman.hungeroverhaul.config.IguanaConfig;
import iguanaman.hungeroverhaul.util.PlantGrowthModification;
import iguanaman.hungeroverhaul.util.RandomHelper;
import java.util.HashMap;
import java.util.Map;
import net.minecraft.block.Block;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.common.BiomeDictionary;
import squeek.applecore.api.plants.PlantGrowthEvent;
import cpw.mods.fml.common.eventhandler.Event.Result;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
public class ModulePlantGrowth
{
private static HashMap<Class<? extends Block>, PlantGrowthModification> plantGrowthModificationsByBlockClass = new HashMap<Class<? extends Block>, PlantGrowthModification>();
private static HashMap<Block, PlantGrowthModification> plantGrowthModificationsByBlock = new HashMap<Block, PlantGrowthModification>();
public static void registerPlantGrowthModifier(Class<? extends Block> blockClass, PlantGrowthModification growthModification)
{
plantGrowthModificationsByBlockClass.put(blockClass, growthModification);
}
public static void registerPlantGrowthModifier(Block block, PlantGrowthModification growthModification)
{
plantGrowthModificationsByBlock.put(block, growthModification);
}
public static PlantGrowthModification getPlantGrowthModification(Block block)
{
if (block == null)
return null;
PlantGrowthModification growthModification = plantGrowthModificationsByBlock.get(block);
if (growthModification != null)
return growthModification;
else
return getPlantGrowthModification(block.getClass());
}
public static PlantGrowthModification getPlantGrowthModification(Class<? extends Block> blockClass)
{
// get exact matches first
PlantGrowthModification growthModifier = plantGrowthModificationsByBlockClass.get(blockClass);
// as a backup, check instanceof
if (growthModifier == null)
{
for (Map.Entry<Class<? extends Block>, PlantGrowthModification> entry : plantGrowthModificationsByBlockClass.entrySet())
{
if (entry.getKey().isAssignableFrom(blockClass))
return entry.getValue();
}
}
return growthModifier;
}
@SubscribeEvent
public void allowGrowthTick(PlantGrowthEvent.AllowGrowthTick event)
{
PlantGrowthModification growthModification = getPlantGrowthModification(event.block.getClass());
if (growthModification == null)
return;
// sunlight
float sunlightModifier = !growthModification.needsSunlight || (growthModification.needsSunlight && event.world.isDaytime() && event.world.canBlockSeeTheSky(event.x, event.y, event.z)) ? 1 : IguanaConfig.noSunlightRegrowthMultiplier;
if (sunlightModifier == 0)
{
event.setResult(Result.DENY);
return;
}
// biome
float biomeModifier = growthModification.wrongBiomeMultiplier;
if (!growthModification.biomeGrowthModifiers.isEmpty())
{
biomeModifier = IguanaConfig.wrongBiomeRegrowthMultiplier;
BiomeGenBase biome = event.world.getBiomeGenForCoords(event.x, event.z);
for (BiomeDictionary.Type type : BiomeDictionary.getTypesForBiome(biome))
{
if (growthModification.biomeGrowthModifiers.containsKey(type))
{
biomeModifier = growthModification.getBiomeGrowthModifier(type);
break;
}
}
}
if (biomeModifier == 0)
{
event.setResult(Result.DENY);
return;
}
// random
if (RandomHelper.nextFloat(event.random, growthModification.growthTickProbability * biomeModifier * sunlightModifier) >= 1)
{
event.setResult(Result.DENY);
return;
}
// still go through with the default conditionals
event.setResult(Result.DEFAULT);
}
}