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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
package gregtech.api.net;
import java.nio.charset.StandardCharsets;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.network.INetHandler;
import net.minecraft.network.NetHandlerPlayServer;
import net.minecraft.world.IBlockAccess;
import com.google.common.io.ByteArrayDataInput;
import gregtech.api.enums.SoundResource;
import gregtech.api.items.MetaBaseItem;
import gregtech.api.util.GTUtility;
import gregtech.common.items.behaviors.BehaviourSprayColorInfinite;
import io.netty.buffer.ByteBuf;
public class GTPacketInfiniteSpraycan extends GTPacket {
private Action action;
private int newColor;
private EntityPlayerMP player;
public GTPacketInfiniteSpraycan() {
super();
}
public GTPacketInfiniteSpraycan(Action action) {
super();
this.action = action;
this.newColor = -1;
}
public GTPacketInfiniteSpraycan(Action action, int newColor) {
super();
this.action = action;
this.newColor = newColor;
}
@Override
public byte getPacketID() {
return GTPacketTypes.INFINITE_SPRAYCAN.id;
}
@Override
public void encode(final ByteBuf aOut) {
final byte[] name = action.name()
.getBytes(StandardCharsets.UTF_8);
aOut.writeInt(newColor);
aOut.writeInt(name.length);
aOut.writeBytes(name);
}
@Override
public GTPacket decode(final ByteArrayDataInput aData) {
final int newColor = aData.readInt();
final int length = aData.readInt();
final byte[] name = new byte[length];
aData.readFully(name, 0, length);
return new GTPacketInfiniteSpraycan(Action.valueOf(new String(name, StandardCharsets.UTF_8)), newColor);
}
@Override
public void setINetHandler(final INetHandler aHandler) {
player = ((NetHandlerPlayServer) aHandler).playerEntity;
}
@Override
public void process(final IBlockAccess aWorld) {
ItemStack currentItemStack = player.inventory.getCurrentItem();
if (currentItemStack != null && currentItemStack.getItem() instanceof MetaBaseItem item) {
item.forEachBehavior(
currentItemStack,
behavior -> behavior instanceof BehaviourSprayColorInfinite spraycanBehavior
&& action.execute(spraycanBehavior, currentItemStack, player, newColor));
}
}
public enum Action {
INCREMENT_COLOR {
@Override
boolean execute(final BehaviourSprayColorInfinite behaviour, final ItemStack itemStack,
final EntityPlayerMP player, final int newColor) {
if (!behaviour.isLocked(itemStack)) {
behaviour.incrementColor(itemStack, player.isSneaking());
playShakeSound(player);
return true;
}
return false;
}
},
LOCK_CAN {
@Override
boolean execute(final BehaviourSprayColorInfinite behavior, final ItemStack itemStack,
final EntityPlayerMP player, final int newColor) {
if (behavior.toggleLock(itemStack)) {
Action.playLockSound(player);
} else {
Action.playUnlockSound(player);
}
return true;
}
},
SET_COLOR {
@Override
boolean execute(final BehaviourSprayColorInfinite behavior, final ItemStack itemStack,
final EntityPlayerMP player, final int newColor) {
if (newColor != -1) {
behavior.setColor(itemStack, (byte) newColor);
Action.playShakeSound(player);
return true;
}
return false;
}
},
TOGGLE_SHAKE_LOCK {
@Override
boolean execute(final BehaviourSprayColorInfinite behavior, final ItemStack itemStack,
final EntityPlayerMP player, final int newColor) {
if (behavior.togglePreventShake(itemStack)) {
Action.playLockSound(player);
} else {
Action.playUnlockSound(player);
}
return true;
}
};
private static void playShakeSound(final EntityPlayerMP player) {
GTUtility.sendSoundToPlayers(
player.worldObj,
SoundResource.GT_SPRAYCAN_SHAKE,
1.0F,
1,
(int) player.posX,
(int) player.posY,
(int) player.posZ);
}
private static void playLockSound(final EntityPlayerMP player) {
GTUtility.sendSoundToPlayers(
player.worldObj,
SoundResource.GT_SPRAYCAN_LOCK,
1.0F,
1,
(int) player.posX,
(int) player.posY,
(int) player.posZ);
}
private static void playUnlockSound(final EntityPlayerMP player) {
GTUtility.sendSoundToPlayers(
player.worldObj,
SoundResource.GT_SPRAYCAN_UNLOCK,
1.0F,
1,
(int) player.posX,
(int) player.posY,
(int) player.posZ);
}
abstract boolean execute(final BehaviourSprayColorInfinite behavior, ItemStack itemStack, EntityPlayerMP player,
final int newColor);
}
}
|