-
Notifications
You must be signed in to change notification settings - Fork 497
/
StackRequest.java
executable file
·108 lines (86 loc) · 3.03 KB
/
StackRequest.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
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* The BuildCraft API is distributed under the terms of the MIT License.
* Please check the contents of the license, which should be located
* as "LICENSE.API" in the BuildCraft source code distribution.
*/
package buildcraft.robotics;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.robots.DockingStation;
import buildcraft.api.robots.IRequestProvider;
import buildcraft.api.robots.IRobotRegistry;
import buildcraft.api.robots.ResourceId;
import buildcraft.api.robots.ResourceIdRequest;
import buildcraft.api.robots.RobotManager;
public class StackRequest {
private IRequestProvider requester;
private int slot;
private ItemStack stack;
private DockingStation station;
private BlockIndex stationIndex;
private ForgeDirection stationSide;
public StackRequest(IRequestProvider requester, int slot, ItemStack stack) {
this.requester = requester;
this.slot = slot;
this.stack = stack;
this.station = null;
}
private StackRequest(int slot, ItemStack stack, BlockIndex stationIndex, ForgeDirection stationSide) {
requester = null;
this.slot = slot;
this.stack = stack;
station = null;
this.stationIndex = stationIndex;
this.stationSide = stationSide;
}
public IRequestProvider getRequester(World world) {
if (requester == null) {
requester = getStation(world).getRequestProvider();
}
return requester;
}
public int getSlot() {
return slot;
}
public ItemStack getStack() {
return stack;
}
public DockingStation getStation(World world) {
if (station == null) {
IRobotRegistry robotRegistry = RobotManager.registryProvider.getRegistry(world);
station = robotRegistry.getStation(stationIndex.x, stationIndex.y, stationIndex.z, stationSide);
}
return station;
}
public void setStation(DockingStation station) {
this.station = station;
this.stationIndex = station.index();
this.stationSide = station.side();
}
public void writeToNBT(NBTTagCompound nbt) {
nbt.setInteger("slot", slot);
NBTTagCompound stackNBT = new NBTTagCompound();
stack.writeToNBT(stackNBT);
nbt.setTag("stack", stackNBT);
NBTTagCompound stationIndexNBT = new NBTTagCompound();
station.index().writeTo(stationIndexNBT);
nbt.setTag("stationIndex", stationIndexNBT);
nbt.setByte("stationSide", (byte) station.side().ordinal());
}
public static StackRequest loadFromNBT(NBTTagCompound nbt) {
int slot = nbt.getInteger("slot");
ItemStack stack = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("stack"));
BlockIndex stationIndex = new BlockIndex(nbt.getCompoundTag("stationIndex"));
ForgeDirection stationSide = ForgeDirection.values()[nbt.getByte("stationSide")];
return new StackRequest(slot, stack, stationIndex, stationSide);
}
public ResourceId getResourceId(World world) {
return new ResourceIdRequest(getStation(world), slot);
}
}