|
2 | 2 |
|
3 | 3 | import com.google.common.collect.Maps;
|
4 | 4 | import com.tomboshoven.minecraft.magicmirror.blocks.tileentities.TileEntityMagicMirrorBase;
|
| 5 | +import com.tomboshoven.minecraft.magicmirror.blocks.tileentities.modifiers.MagicMirrorTileEntityModifier; |
| 6 | +import mcp.MethodsReturnNonnullByDefault; |
5 | 7 | import net.minecraft.item.ItemStack;
|
6 | 8 | import net.minecraft.nbt.NBTTagCompound;
|
7 | 9 | import net.minecraft.tileentity.TileEntity;
|
|
10 | 12 | import net.minecraft.world.World;
|
11 | 13 |
|
12 | 14 | import javax.annotation.Nullable;
|
| 15 | +import javax.annotation.ParametersAreNonnullByDefault; |
13 | 16 | import java.util.Collection;
|
14 | 17 | import java.util.Collections;
|
15 | 18 | import java.util.Map;
|
|
20 | 23 | * These are typically activated by right-clicking a mirror with an item.
|
21 | 24 | * In order to make a modifier available in the game, register it using the static register() method.
|
22 | 25 | */
|
| 26 | +@ParametersAreNonnullByDefault |
| 27 | +@MethodsReturnNonnullByDefault |
23 | 28 | public abstract class MagicMirrorModifier {
|
24 | 29 | /**
|
25 | 30 | * A registry of all modifiers.
|
@@ -53,6 +58,38 @@ public static MagicMirrorModifier getModifier(String name) {
|
53 | 58 | return MODIFIERS.get(name);
|
54 | 59 | }
|
55 | 60 |
|
| 61 | + /** |
| 62 | + * Helper method to get a magic mirror tile entity at the given position. |
| 63 | + * |
| 64 | + * @param worldIn The world containing the magic mirror. |
| 65 | + * @param pos The position in the world of the block. |
| 66 | + * @return The tile entity, or null if it does not exist or is not a magic mirror tile entity. |
| 67 | + */ |
| 68 | + @Nullable |
| 69 | + private static TileEntityMagicMirrorBase getMagicMirrorTileEntity(IBlockAccess worldIn, BlockPos pos) { |
| 70 | + TileEntity tileEntity = worldIn.getTileEntity(pos); |
| 71 | + if (tileEntity instanceof TileEntityMagicMirrorBase) { |
| 72 | + return (TileEntityMagicMirrorBase) tileEntity; |
| 73 | + } |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Find out whether the mirror at the given position already has a modifier of a given type. |
| 79 | + * |
| 80 | + * @param world The world in which to check the block. |
| 81 | + * @param pos The position of the mirror block to check. |
| 82 | + * @param modifierType The type of modifier to test for. |
| 83 | + * @return Whether the mirror at the given position has the given modifier. |
| 84 | + */ |
| 85 | + private static boolean hasModifierOfType(IBlockAccess world, BlockPos pos, Class<? extends MagicMirrorModifier> modifierType) { |
| 86 | + TileEntityMagicMirrorBase magicMirrorTileEntity = getMagicMirrorTileEntity(world, pos); |
| 87 | + if (magicMirrorTileEntity == null) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + return magicMirrorTileEntity.getModifiers().stream().anyMatch(modifierType::isInstance); |
| 91 | + } |
| 92 | + |
56 | 93 | /**
|
57 | 94 | * @return The name of the modifier.
|
58 | 95 | */
|
@@ -95,29 +132,36 @@ public void apply(IBlockAccess worldIn, BlockPos pos, ItemStack heldItem) {
|
95 | 132 | * @param tileEntity The magic mirror tile entity to apply the modifier to.
|
96 | 133 | * @param heldItem The item used on the block.
|
97 | 134 | */
|
98 |
| - abstract void apply(TileEntityMagicMirrorBase tileEntity, ItemStack heldItem); |
| 135 | + private void apply(TileEntityMagicMirrorBase tileEntity, ItemStack heldItem) { |
| 136 | + tileEntity.addModifier(createTileEntityModifier()); |
| 137 | + heldItem.shrink(1); |
| 138 | + } |
99 | 139 |
|
100 | 140 | /**
|
101 | 141 | * Apply the modifier to the magic mirror as specified in an NBT tag.
|
102 | 142 | *
|
103 | 143 | * @param tileEntity The magic mirror tile entity to apply the modifier to.
|
104 | 144 | * @param nbt The NBT tag to use for the modifier.
|
105 | 145 | */
|
106 |
| - public abstract void apply(TileEntityMagicMirrorBase tileEntity, NBTTagCompound nbt); |
| 146 | + public void apply(TileEntityMagicMirrorBase tileEntity, NBTTagCompound nbt) { |
| 147 | + MagicMirrorTileEntityModifier magicMirrorTileEntityModifier = createTileEntityModifier(); |
| 148 | + magicMirrorTileEntityModifier.readFromNBT(nbt); |
| 149 | + tileEntity.addModifier(magicMirrorTileEntityModifier); |
| 150 | + } |
107 | 151 |
|
108 | 152 | /**
|
109 |
| - * Helper method to get a magic mirror tile entity at the given position. |
| 153 | + * @return A new instance of the tile entity modifier. |
| 154 | + */ |
| 155 | + abstract MagicMirrorTileEntityModifier createTileEntityModifier(); |
| 156 | + |
| 157 | + /** |
| 158 | + * Find out whether the mirror at the given position already has a modifier of the current type. |
110 | 159 | *
|
111 |
| - * @param worldIn The world containing the magic mirror. |
112 |
| - * @param pos The position in the world of the block. |
113 |
| - * @return The tile entity, or null if it does not exist or is not a magic mirror tile entity. |
| 160 | + * @param world The world in which to check the block. |
| 161 | + * @param pos The position of the mirror block to check. |
| 162 | + * @return Whether the mirror at the given position has the current modifier. |
114 | 163 | */
|
115 |
| - @Nullable |
116 |
| - static TileEntityMagicMirrorBase getMagicMirrorTileEntity(IBlockAccess worldIn, BlockPos pos) { |
117 |
| - TileEntity tileEntity = worldIn.getTileEntity(pos); |
118 |
| - if (tileEntity instanceof TileEntityMagicMirrorBase) { |
119 |
| - return (TileEntityMagicMirrorBase) tileEntity; |
120 |
| - } |
121 |
| - return null; |
| 164 | + boolean hasModifierOfType(IBlockAccess world, BlockPos pos) { |
| 165 | + return hasModifierOfType(world, pos, getClass()); |
122 | 166 | }
|
123 | 167 | }
|
0 commit comments