summaryrefslogtreecommitdiff
path: root/src/api/java/thaumcraft/api/wands/WandTriggerRegistry.java
blob: c9c24f277b3aa661295ace4dd3905bcb855e1d22 (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
package thaumcraft.api.wands;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

/**
 * This class serves a similar function to IWandable in that it allows wands to interact
 * with object in the world. In this case it is most useful for adding interaction with non-mod
 * blocks where you can't control what happens in their code.
 * Example where it is used is in crafting the thaumonomicon from a bookshelf and the
 * crucible from a cauldron
 * 
 * @author azanor
 *
 */
public class WandTriggerRegistry
{

	private static HashMap<String, HashMap<List, List>>	triggers	= new HashMap<String, HashMap<List, List>>();
	private static final String							DEFAULT		= "default";

	/**
	 * Registers an action to perform when a casting wand right clicks on a specific block. 
	 * A manager class needs to be created that implements IWandTriggerManager.
	 * @param manager
	 * @param event a logical number that you can use to differentiate different events or actions
	 * @param block
	 * @param meta send -1 as a wildcard value for all possible meta values
	 * @param modid a unique identifier. It is best to register your own triggers using your mod id to avoid conflicts with mods that register triggers for the same block
	 */
	public static void registerWandBlockTrigger(IWandTriggerManager manager, int event, Block block, int meta, String modid)
	{
		if(!triggers.containsKey(modid))
		{
			triggers.put(modid, new HashMap<List, List>());
		}
		final HashMap<List, List> temp = triggers.get(modid);
		temp.put(Arrays.asList(block, meta), Arrays.asList(manager, event));
		triggers.put(modid, temp);
	}

	/**
	 * for legacy support
	 */
	public static void registerWandBlockTrigger(IWandTriggerManager manager, int event, Block block, int meta)
	{
		registerWandBlockTrigger(manager, event, block, meta, DEFAULT);
	}

	/**
	 * Checks all trigger registries if one exists for the given block and meta
	 * @param block
	 * @param meta
	 * @return
	 */
	public static boolean hasTrigger(Block block, int meta)
	{
		for(final String modid : triggers.keySet())
		{
			final HashMap<List, List> temp = triggers.get(modid);
			if(temp.containsKey(Arrays.asList(block, meta)) || temp.containsKey(Arrays.asList(block, -1)))
			{
				return true;
			}
		}
		return false;
	}

	/**
	 * modid sensitive version
	 */
	public static boolean hasTrigger(Block block, int meta, String modid)
	{
		if(!triggers.containsKey(modid))
		{
			return false;
		}
		final HashMap<List, List> temp = triggers.get(modid);
		if(temp.containsKey(Arrays.asList(block, meta)) || temp.containsKey(Arrays.asList(block, -1)))
		{
			return true;
		}
		return false;
	}

	/**
	 * This is called by the onItemUseFirst function in wands. 
	 * Parameters and return value functions like you would expect for that function.
	 * @param world
	 * @param wand
	 * @param player
	 * @param x
	 * @param y
	 * @param z
	 * @param side
	 * @param block
	 * @param meta
	 * @return
	 */
	public static boolean performTrigger(World world, ItemStack wand, EntityPlayer player, int x, int y, int z, int side, Block block, int meta)
	{

		for(final String modid : triggers.keySet())
		{
			final HashMap<List, List> temp = triggers.get(modid);
			List l = temp.get(Arrays.asList(block, meta));
			if(l == null)
			{
				l = temp.get(Arrays.asList(block, -1));
			}
			if(l == null)
			{
				continue;
			}

			final IWandTriggerManager manager = (IWandTriggerManager) l.get(0);
			final int event = (Integer) l.get(1);
			final boolean result = manager.performTrigger(world, wand, player, x, y, z, side, event);
			if(result)
			{
				return true;
			}
		}
		return false;
	}

	/**
	 * modid sensitive version
	 */
	public static boolean performTrigger(World world, ItemStack wand, EntityPlayer player, int x, int y, int z, int side, Block block, int meta, String modid)
	{
		if(!triggers.containsKey(modid))
		{
			return false;
		}
		final HashMap<List, List> temp = triggers.get(modid);
		List l = temp.get(Arrays.asList(block, meta));
		if(l == null)
		{
			l = temp.get(Arrays.asList(block, -1));
		}
		if(l == null)
		{
			return false;
		}

		final IWandTriggerManager manager = (IWandTriggerManager) l.get(0);
		final int event = (Integer) l.get(1);
		return manager.performTrigger(world, wand, player, x, y, z, side, event);
	}

}