aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kekztech/MultiItemHandler.java
blob: 766abbbb93db8bb8f49944fc848245b7e195ee9d (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
package kekztech;

import java.util.ArrayList;

import net.minecraft.item.ItemStack;

public class MultiItemHandler {
	
	private int perTypeCapacity = 0; 
	
	private boolean locked = true;
	
	private final ArrayList<ItemStack> items = new ArrayList<>();
	
	public MultiItemHandler() {
		
	}
	
	/**
	 * Tries to adapt the internal storage to match structure changes.
	 * Structure should turn off and give a warning if this returns false. 
	 * Otherwise items might unavailable.
	 * 
	 * @param itemTypeCapacity
	 * 			New item array length to adapt to.
	 * @return Success status of the operation. 
	 */
	public boolean setItemTypeCapacity(int itemTypeCapacity) {
		if(items.size() > itemTypeCapacity) {
			System.out.println("WARNING: ITEM SERVER STRUCTURE WAS DOWNSIZED TOO FAR! LOCKING FOR SAFETY.");
			setLock(true);
			return false;
		} else {
			items.ensureCapacity(itemTypeCapacity);
			// If the lock was engaged, it should only be disengaged by turning 
			// the structure back on after fixing the above warning.
			return true;
		}
	}
	
	public void setPerTypeCapacity(int perTypeCapacity) {
		this.perTypeCapacity = perTypeCapacity;
	}
	
	/**
	 * Lock internal storage in case Item Server is not running.
	 * May also be engaged in case of item safety issues.
	 * 
	 * @param state
	 * 				Lock state.
	 */
	public void setLock(boolean state) {
		locked = state;
	}
	
	public int getItemTypeCapacity() {
		return items.size();
	}
	
	public int getPerTypeCapacity() {
		return perTypeCapacity;
	}
	
	public ItemStack getStackInSlot(int slot) {
		System.out.println("Stack in slot " + slot + " requested");
		if(locked || slot >= items.size()) {
			return null;
		} else {
			return items.get(slot);
		}
	}
	
	public void insertStackInSlot(int slot, ItemStack itemStack) {
		System.out.println("Inserting " + itemStack.getDisplayName() + " into " + slot);
		if(itemStack == null 
				|| items.get(slot) != null
				|| locked
				|| slot >= items.size()) {
			return;
		} else {
			items.set(slot, itemStack);
		}
	}
	
	public int increaseStackInSlot(int slot, int amount) {
		System.out.println("Increasing item in slot " + slot + " by " + amount);
		if(slot >= items.size()
				|| locked
				|| amount <= 0) {
			return 0;
		} else {
			final int space = perTypeCapacity - items.get(slot).stackSize;
			final int fit = Math.min(space, amount);
			items.get(slot).stackSize += fit;
			return fit;
		}
	}
	
	public int reduceStackInSlot(int slot, int amount) {
		System.out.println("Reducing item in slot " + slot + " by " + amount);
		if(slot >= items.size()
				|| locked
				|| amount <= 0) {
			return 0;
		} else {
			final int available = items.get(slot).stackSize;
			final int take = Math.min(available, amount);
			items.get(slot).stackSize -= take;
			if(take == available) {
				items.set(slot, null);
			}
			return take;
		}
	}
	
	
}