|
| 1 | +package clashsoft.playerinventoryapi; |
| 2 | + |
| 3 | +import gnu.trove.set.hash.TCustomHashSet; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +import clashsoft.cslib.math.Point2i; |
| 11 | +import clashsoft.playerinventoryapi.api.IInventoryHandler; |
| 12 | +import clashsoft.playerinventoryapi.api.invobject.IInventoryObject; |
| 13 | +import clashsoft.playerinventoryapi.inventory.SlotConstants; |
| 14 | +import clashsoft.playerinventoryapi.lib.ButtonHashingStrategy; |
| 15 | + |
| 16 | +public class CreativeInventory implements SlotConstants |
| 17 | +{ |
| 18 | + protected static Point2i[] slots; |
| 19 | + |
| 20 | + public static int windowWidth = 195; |
| 21 | + public static int windowHeight = 136; |
| 22 | + public static int playerDisplayX = 8; |
| 23 | + public static int playerDisplayY = 5; |
| 24 | + public static int binSlotX = 173; |
| 25 | + public static int binSlotY = 112; |
| 26 | + |
| 27 | + public static Set buttons = new TCustomHashSet(ButtonHashingStrategy.instance); |
| 28 | + public static List<IInventoryHandler> handlers = new ArrayList(); |
| 29 | + public static List<IInventoryObject> objects = new ArrayList(); |
| 30 | + |
| 31 | + public static Point2i[] getSlots() |
| 32 | + { |
| 33 | + if (slots == null) |
| 34 | + { |
| 35 | + slots = new Point2i[64]; |
| 36 | + getSlots(slots); |
| 37 | + } |
| 38 | + return slots; |
| 39 | + } |
| 40 | + |
| 41 | + protected static void getSlots(Point2i[] pos) |
| 42 | + { |
| 43 | + int i; |
| 44 | + int j; |
| 45 | + |
| 46 | + pos[CRAFTING_OUTPUT] = new Point2i(-2000, -2000); |
| 47 | + pos[CRAFTING_TOP_LEFT] = new Point2i(-2000, -2000); |
| 48 | + pos[CRAFTING_TOP_RIGHT] = new Point2i(-2000, -2000); |
| 49 | + pos[CRAFTING_BOTTOM_LEFT] = new Point2i(-2000, -2000); |
| 50 | + pos[CRAFTING_BOTTOM_RIGHT] = new Point2i(-2000, -2000); |
| 51 | + |
| 52 | + pos[HELMET] = new Point2i(45, 6); |
| 53 | + pos[CHESTPLATE] = new Point2i(63, 6); |
| 54 | + pos[LEGGINGS] = new Point2i(81, 6); |
| 55 | + pos[BOOTS] = new Point2i(99, 6); |
| 56 | + |
| 57 | + // 9 - 35 = Inventory |
| 58 | + for (i = 0; i < 3; ++i) |
| 59 | + { |
| 60 | + for (j = 0; j < 9; ++j) |
| 61 | + { |
| 62 | + pos[9 + j + i * 9] = new Point2i(9 + j * 18, 54 + i * 18); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // 36 - 44 = Hotbar |
| 67 | + for (i = 0; i < 9; ++i) |
| 68 | + { |
| 69 | + pos[36 + i] = new Point2i(9 + i * 18, 112); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Sets the position of a slot in the creative player inventory. |
| 75 | + * |
| 76 | + * @param slotID |
| 77 | + * the ID of the slot |
| 78 | + * @param x |
| 79 | + * the x position |
| 80 | + * @param y |
| 81 | + * the y position |
| 82 | + */ |
| 83 | + public static void setSlot(int slotID, int x, int y) |
| 84 | + { |
| 85 | + Point2i[] array = slots; |
| 86 | + if (slotID >= array.length) |
| 87 | + { |
| 88 | + array = Arrays.copyOf(array, slotID + 1); |
| 89 | + slots = array; |
| 90 | + } |
| 91 | + |
| 92 | + if (array[slotID] == null) |
| 93 | + { |
| 94 | + array[slotID] = new Point2i(x, y); |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + array[slotID].setX(x).setY(y); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Sets the position of the player display in the survival inventory. |
| 104 | + * |
| 105 | + * @param x |
| 106 | + * the x position |
| 107 | + * @param y |
| 108 | + * the y position |
| 109 | + */ |
| 110 | + public static void setPlayer(int x, int y) |
| 111 | + { |
| 112 | + playerDisplayX = x; |
| 113 | + playerDisplayY = y; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Sets the position of the bin slot in the creative inventory. |
| 118 | + * |
| 119 | + * @param x |
| 120 | + * the x position |
| 121 | + * @param y |
| 122 | + * the y position |
| 123 | + */ |
| 124 | + public static void setBinSlot(int x, int y) |
| 125 | + { |
| 126 | + binSlotX = x; |
| 127 | + binSlotY = y; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Sets the position of the armor slots in the creative inventory. |
| 132 | + * |
| 133 | + * @param x |
| 134 | + * the x position |
| 135 | + * @param y |
| 136 | + * the y position |
| 137 | + */ |
| 138 | + public static void setArmor(int x, int y) |
| 139 | + { |
| 140 | + setSlot(HELMET, x, y); |
| 141 | + setSlot(CHESTPLATE, x + 18, y); |
| 142 | + setSlot(LEGGINGS, x + 36, y); |
| 143 | + setSlot(BOOTS, x + 54, y); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Sets the size of the creative inventory window. |
| 148 | + * |
| 149 | + * @param width |
| 150 | + * the width |
| 151 | + * @param height |
| 152 | + * the height |
| 153 | + */ |
| 154 | + public static void setWindowSize(int width, int height) |
| 155 | + { |
| 156 | + windowWidth = width; |
| 157 | + windowHeight = height; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Registers a new {@link IInventoryHandler}. |
| 162 | + * |
| 163 | + * @param handler |
| 164 | + */ |
| 165 | + public static void addHandler(IInventoryHandler handler) |
| 166 | + { |
| 167 | + handlers.add(handler); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Adds a button to the creative inventory. |
| 172 | + * |
| 173 | + * @param button |
| 174 | + * the button |
| 175 | + */ |
| 176 | + public static void addButton(Object button) |
| 177 | + { |
| 178 | + buttons.add(button); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Adds an {@link IInventoryObject} to the creative inventory. |
| 183 | + * |
| 184 | + * @param object |
| 185 | + * the object |
| 186 | + */ |
| 187 | + public static void addObject(IInventoryObject object) |
| 188 | + { |
| 189 | + objects.add(object); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Resets the creative inventory to its vanilla state. This resets |
| 194 | + * <ul> |
| 195 | + * <li>The window size |
| 196 | + * <li>The player display position |
| 197 | + * <li>The bin slot position |
| 198 | + * <li>The positions of all vanilla slots |
| 199 | + * <li>The buttons |
| 200 | + * <li>The renderable objects. |
| 201 | + * </ul> |
| 202 | + */ |
| 203 | + public static void reset() |
| 204 | + { |
| 205 | + windowWidth = 195; |
| 206 | + windowHeight = 136; |
| 207 | + playerDisplayX = 8; |
| 208 | + playerDisplayY = 5; |
| 209 | + binSlotX = 173; |
| 210 | + binSlotY = 112; |
| 211 | + |
| 212 | + getSlots(slots); |
| 213 | + |
| 214 | + buttons.clear(); |
| 215 | + objects.clear(); |
| 216 | + } |
| 217 | +} |
0 commit comments