Skip to content

Commit 0ad4a3e

Browse files
authored
Merge pull request #143 from Ugachaga/1.7.10
More work on packets
2 parents 745f9d5 + 9cb8c6f commit 0ad4a3e

13 files changed

Lines changed: 184 additions & 269 deletions

src/main/java/com/darkona/adventurebackpack/common/BackpackAbilities.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ public void itemCreeper(EntityPlayer player, World world, ItemStack backpack)
498498
*/
499499
public void itemCow(EntityPlayer player, World world, ItemStack backpack)
500500
{
501-
if (world.isRemote) return; //TODO not syncing properly with client if GUI is open
501+
if (world.isRemote) return; //TODO not syncing properly with client if GUI is open (see unused CowAbilityPacket?)
502502
InventoryBackpack inv = new InventoryBackpack(backpack);
503503
inv.openInventory();
504504

src/main/java/com/darkona/adventurebackpack/common/ServerActions.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ public class ServerActions
5555
* Cycles tools. In a cycle. The tool in your hand with the tools in the special tool playerSlot of the backpack,
5656
* to be precise.
5757
*
58-
* @param player Duh
59-
* @param isWheelUp A boolean indicating the direction of the switch. Nobody likes to swith always in the same
60-
* direction all the timeInSeconds. That's stupid.
61-
* @param playerSlot The slot that will be switched with the backpack.
58+
* @param player Duh
59+
* @param isWheelUp A boolean indicating the direction of the switch. Nobody likes to swith always in the same
60+
* direction all the timeInSeconds. That's stupid.
6261
*/
63-
public static void cycleTool(EntityPlayer player, boolean isWheelUp, int playerSlot)
62+
public static void cycleTool(EntityPlayer player, boolean isWheelUp)
6463
{
6564
if (!GeneralReference.isDimensionAllowed(player))
6665
return;
6766

67+
int playerSlot = player.inventory.currentItem;
6868
ItemStack current = player.getCurrentEquippedItem();
6969
if (SlotTool.isValidTool(current))
7070
{
@@ -81,7 +81,7 @@ public static void cycleTool(EntityPlayer player, boolean isWheelUp, int playerS
8181
* @param player Is a player. To whom the nice or evil effects you're going to apply will affect.
8282
* See? I know the proper use of the words "effect" & "affect".
8383
* @param tank The tank that holds the fluid, whose effect will affect the player that's in the world.
84-
* @return If the effect can be applied, and it is actually applied, returns true.
84+
* @return If the effect can be applied, and it is actually applied, returns true.
8585
*/
8686
public static boolean setFluidEffect(World world, EntityPlayer player, FluidTank tank)
8787
{
@@ -149,7 +149,7 @@ public static void electrify(EntityPlayer player)
149149
{
150150
BackpackUtils.setBackpackType(backpack, BackpackTypes.PIGMAN);
151151
}
152-
if (BackpackTypes.getType(backpack) == BackpackTypes.DIAMOND)
152+
else if (BackpackTypes.getType(backpack) == BackpackTypes.DIAMOND)
153153
{
154154
BackpackUtils.setBackpackType(backpack, BackpackTypes.ELECTRIC);
155155
}
@@ -207,17 +207,17 @@ public static void leakArrow(EntityPlayer player, ItemStack bow, int charge)
207207
else
208208
{
209209
/*
210-
* From here, instead of leaking an arrow to the player inventory, which may be full and then it would be
211-
* pointless, leak an arrow straight from the backpack ^_^
212-
*
213-
* It could be possible to switch a whole stack with the player inventory, fire the arrow, and then
214-
* switch back, but that's stupid.
215-
*
216-
* That's how you make a quiver (for vanilla bows at least, or anything that uses the events and vanilla
217-
* arrows) Until we have an event that fires when a player consumes items in his/her inventory.
218-
*
219-
* I should make a pull request. Too lazy, though.
220-
* */
210+
* From here, instead of leaking an arrow to the player inventory, which may be full and then it would be
211+
* pointless, leak an arrow straight from the backpack ^_^
212+
*
213+
* It could be possible to switch a whole stack with the player inventory, fire the arrow, and then
214+
* switch back, but that's stupid.
215+
*
216+
* That's how you make a quiver (for vanilla bows at least, or anything that uses the events and vanilla
217+
* arrows) Until we have an event that fires when a player consumes items in his/her inventory.
218+
*
219+
* I should make a pull request. Too lazy, though.
220+
* */
221221
backpack.consumeInventoryItem(Items.arrow);
222222
backpack.dirtyInventory();
223223
}

src/main/java/com/darkona/adventurebackpack/handlers/ClientEventHandler.java

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212

1313
import com.darkona.adventurebackpack.common.ServerActions;
1414
import com.darkona.adventurebackpack.config.ConfigHandler;
15+
import com.darkona.adventurebackpack.init.ModItems;
1516
import com.darkona.adventurebackpack.init.ModNetwork;
1617
import com.darkona.adventurebackpack.inventory.SlotTool;
17-
import com.darkona.adventurebackpack.item.ItemAdventureBackpack;
18-
import com.darkona.adventurebackpack.item.ItemHose;
1918
import com.darkona.adventurebackpack.network.CycleToolPacket;
2019
import com.darkona.adventurebackpack.reference.BackpackTypes;
2120
import com.darkona.adventurebackpack.util.Wearing;
@@ -32,38 +31,30 @@ public class ClientEventHandler
3231
public void mouseWheelDetect(MouseEvent event)
3332
{
3433
/*Special thanks go to MachineMuse, both for inspiration and the event. God bless you girl.*/
35-
Minecraft mc = Minecraft.getMinecraft();
36-
if (event.dwheel != 0)
37-
{
38-
boolean isWheelUp = event.dwheel > 0;
39-
EntityClientPlayerMP player = mc.thePlayer;
40-
if (player != null && !player.isDead && player.isSneaking())
41-
{
42-
ItemStack backpack = Wearing.getWearingBackpack(player);
43-
if (backpack != null && backpack.getItem() instanceof ItemAdventureBackpack)
44-
{
45-
if (player.getCurrentEquippedItem() != null)
46-
{
47-
int slot = player.inventory.currentItem;
48-
ItemStack heldItem = player.inventory.getStackInSlot(slot);
49-
Item theItem = heldItem.getItem();
34+
EntityClientPlayerMP player = Minecraft.getMinecraft().thePlayer;
35+
if (event.dwheel == 0 || player == null || player.isDead || !player.isSneaking())
36+
return;
37+
38+
ItemStack backpack = Wearing.getWearingBackpack(player);
39+
ItemStack heldItem = player.getCurrentEquippedItem();
40+
if (backpack == null || heldItem == null)
41+
return;
42+
43+
boolean isWheelUp = event.dwheel > 0;
44+
Item theItem = heldItem.getItem();
5045

51-
if ((ConfigHandler.enableToolsCycling && !Wearing.getWearingBackpackInv(player).getDisableCycling() && SlotTool.isValidTool(heldItem))
52-
|| (BackpackTypes.getType(backpack) == BackpackTypes.SKELETON && theItem.equals(Items.bow)))
53-
{
54-
ModNetwork.net.sendToServer(new CycleToolPacket.CycleToolMessage(isWheelUp, slot, CycleToolPacket.CYCLE_TOOL_ACTION));
55-
ServerActions.cycleTool(player, isWheelUp, slot);
56-
event.setCanceled(true);
57-
}
58-
else if (theItem instanceof ItemHose)
59-
{
60-
ModNetwork.net.sendToServer(new CycleToolPacket.CycleToolMessage(isWheelUp, slot, CycleToolPacket.SWITCH_HOSE_ACTION));
61-
ServerActions.switchHose(player, isWheelUp, ServerActions.HOSE_SWITCH);
62-
event.setCanceled(true);
63-
}
64-
}
65-
}
66-
}
46+
if (ConfigHandler.enableToolsCycling && !Wearing.getWearingBackpackInv(player).getDisableCycling() && SlotTool.isValidTool(heldItem)
47+
|| BackpackTypes.getType(backpack) == BackpackTypes.SKELETON && theItem.equals(Items.bow)) //TODO add bow case to server
48+
{
49+
ModNetwork.net.sendToServer(new CycleToolPacket.CycleToolMessage(isWheelUp, CycleToolPacket.CYCLE_TOOL_ACTION));
50+
ServerActions.cycleTool(player, isWheelUp);
51+
event.setCanceled(true);
52+
}
53+
else if (theItem == ModItems.hose)
54+
{
55+
ModNetwork.net.sendToServer(new CycleToolPacket.CycleToolMessage(isWheelUp, CycleToolPacket.SWITCH_HOSE_ACTION));
56+
ServerActions.switchHose(player, isWheelUp, ServerActions.HOSE_SWITCH);
57+
event.setCanceled(true);
6758
}
6859
}
6960
}

src/main/java/com/darkona/adventurebackpack/handlers/KeyInputEventHandler.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
/**
2121
* Created by Darkona on 11/10/2014.
2222
*/
23-
2423
public class KeyInputEventHandler
2524
{
2625
public enum Key
@@ -35,7 +34,7 @@ public void handleKeyInputEvent(InputEvent.KeyInputEvent event)
3534
Minecraft mc = Minecraft.getMinecraft();
3635
EntityPlayer player = mc.thePlayer;
3736

38-
if (pressedKey == Key.UNKNOWN || player == null)
37+
if (pressedKey == Key.UNKNOWN || player == null || player.isDead)
3938
return;
4039

4140
if (pressedKey == Key.INVENTORY && mc.inGameHasFocus)
@@ -59,13 +58,11 @@ else if (Wearing.isHoldingBackpack(player))
5958
{
6059
sendGUIPacket(GUIPacket.BACKPACK_GUI, GUIPacket.FROM_HOLDING);
6160
}
62-
63-
if (Wearing.isWearingCopter(player))
61+
else if (Wearing.isWearingCopter(player))
6462
{
6563
sendGUIPacket(GUIPacket.COPTER_GUI, GUIPacket.FROM_WEARING);
6664
}
67-
68-
if (Wearing.isWearingJetpack(player))
65+
else if (Wearing.isWearingJetpack(player))
6966
{
7067
sendGUIPacket(GUIPacket.JETPACK_GUI, GUIPacket.FROM_WEARING);
7168
}
@@ -76,7 +73,7 @@ else if (Wearing.isHoldingBackpack(player))
7673
{
7774
if (Wearing.isHoldingHose(player))
7875
{
79-
sendCycleToolPacket(player.inventory.currentItem, CycleToolPacket.TOGGLE_HOSE_TANK);
76+
sendCycleToolPacket(CycleToolPacket.TOGGLE_HOSE_TANK);
8077
ServerActions.switchHose(player, false, ServerActions.HOSE_TOGGLE);
8178
}
8279
else if (Wearing.isWearingBackpack(player))
@@ -95,7 +92,7 @@ else if (Wearing.isWearingBackpack(player))
9592
ServerActions.toggleToolCycling(player, Wearing.getWearingBackpack(player));
9693
}
9794
}
98-
if (Wearing.isWearingCopter(player))
95+
else if (Wearing.isWearingCopter(player))
9996
{
10097
if (player.isSneaking())
10198
{
@@ -108,7 +105,7 @@ else if (Wearing.isWearingBackpack(player))
108105
ServerActions.toggleCopterPack(player, Wearing.getWearingCopter(player), WearableModePacket.COPTER_TOGGLE);
109106
}
110107
}
111-
if (Wearing.isWearingJetpack(player))
108+
else if (Wearing.isWearingJetpack(player))
112109
{
113110
if (player.isSneaking())
114111
{
@@ -120,7 +117,7 @@ else if (Wearing.isWearingBackpack(player))
120117

121118
if (pressedKey == Key.JUMP)
122119
{
123-
if (player.ridingEntity != null && player.ridingEntity instanceof EntityFriendlySpider)
120+
if (player.ridingEntity instanceof EntityFriendlySpider)
124121
{
125122
sendPlayerActionPacket(PlayerActionPacket.SPIDER_JUMP);
126123
((EntityFriendlySpider) player.ridingEntity).setJumping(true);
@@ -157,12 +154,12 @@ private void sendGUIPacket(byte type, byte from)
157154

158155
private void sendWearableModePacket(byte type)
159156
{
160-
ModNetwork.net.sendToServer(new WearableModePacket.Message(type, "")); //TODO playerID?
157+
ModNetwork.net.sendToServer(new WearableModePacket.Message(type));
161158
}
162159

163-
private void sendCycleToolPacket(int slot, byte type)
160+
private void sendCycleToolPacket(byte type)
164161
{
165-
ModNetwork.net.sendToServer(new CycleToolPacket.CycleToolMessage(false, slot, type));
162+
ModNetwork.net.sendToServer(new CycleToolPacket.CycleToolMessage(false, type));
166163
}
167164

168165
private void sendPlayerActionPacket(byte type)

src/main/java/com/darkona/adventurebackpack/network/CowAbilityPacket.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,10 @@ public IMessage onMessage(CowAbilityMessage message, MessageContext ctx)
4848

4949
public static class CowAbilityMessage implements IMessage
5050
{
51-
5251
private byte action;
5352
private String playerID;
5453

55-
public CowAbilityMessage()
56-
{
57-
}
54+
public CowAbilityMessage() {}
5855

5956
public CowAbilityMessage(String playerID, byte action)
6057
{
@@ -65,7 +62,6 @@ public CowAbilityMessage(String playerID, byte action)
6562
@Override
6663
public void fromBytes(ByteBuf buf)
6764
{
68-
6965
playerID = ByteBufUtils.readUTF8String(buf);
7066
action = buf.readByte();
7167
}

src/main/java/com/darkona/adventurebackpack/network/CycleToolPacket.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ public IMessage onMessage(CycleToolMessage message, MessageContext ctx)
2626
if (ctx.side.isServer())
2727
{
2828
EntityPlayerMP player = ctx.getServerHandler().playerEntity;
29+
30+
if (player == null || player.isDead)
31+
return null;
32+
2933
switch (message.typeOfAction)
3034
{
3135
case CYCLE_TOOL_ACTION:
32-
ServerActions.cycleTool(player, message.isWheelUp, message.slot);
36+
ServerActions.cycleTool(player, message.isWheelUp);
3337
break;
3438
case TOGGLE_HOSE_TANK:
3539
ServerActions.switchHose(player, message.isWheelUp, ServerActions.HOSE_TOGGLE);
@@ -46,34 +50,27 @@ public static class CycleToolMessage implements IMessage
4650
{
4751
private byte typeOfAction;
4852
private boolean isWheelUp;
49-
private int slot;
5053

51-
public CycleToolMessage()
52-
{
53-
54-
}
54+
public CycleToolMessage() {}
5555

56-
public CycleToolMessage(boolean isWheelUp, int slot, byte typeOfAction)
56+
public CycleToolMessage(boolean isWheelUp, byte typeOfAction)
5757
{
5858
this.typeOfAction = typeOfAction;
5959
this.isWheelUp = isWheelUp;
60-
this.slot = slot;
6160
}
6261

6362
@Override
6463
public void fromBytes(ByteBuf buf)
6564
{
6665
this.typeOfAction = buf.readByte();
6766
this.isWheelUp = buf.readBoolean();
68-
this.slot = buf.readInt();
6967
}
7068

7169
@Override
7270
public void toBytes(ByteBuf buf)
7371
{
7472
buf.writeByte(typeOfAction);
7573
buf.writeBoolean(isWheelUp);
76-
buf.writeInt(slot);
7774
}
7875
}
7976
}

src/main/java/com/darkona/adventurebackpack/network/EquipUnequipBackWearablePacket.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,28 @@ public class EquipUnequipBackWearablePacket implements IMessageHandler<EquipUneq
2424
@Override
2525
public Message onMessage(Message message, MessageContext ctx)
2626
{
27-
2827
if (ctx.side.isServer())
2928
{
3029
EntityPlayer player = ctx.getServerHandler().playerEntity;
31-
if (message.action == EQUIP_WEARABLE)
30+
31+
if (player == null || player.isDead)
32+
return null;
33+
34+
if (message.action == EQUIP_WEARABLE && Wearing.isHoldingWearable(player))
3235
{
33-
if (Wearing.isHoldingWearable(player))
36+
if (Wearing.isWearingWearable(player))
37+
{
38+
Wearing.WearableType wtype = Wearing.getWearingWearableType(player);
39+
if (wtype != Wearing.WearableType.UNKNOWN)
40+
player.addChatComponentMessage(new ChatComponentTranslation("adventurebackpack:messages.already.equipped." + wtype.name().toLowerCase()));
41+
}
42+
else if (BackpackUtils.equipWearable(player.getCurrentEquippedItem(), player) == BackpackUtils.Reasons.SUCCESSFUL)
3443
{
35-
if (Wearing.isWearingWearable(player))
36-
{
37-
Wearing.WearableType wtype = Wearing.getWearingWearableType(player);
38-
if (wtype != Wearing.WearableType.UNKNOWN)
39-
player.addChatComponentMessage(new ChatComponentTranslation("adventurebackpack:messages.already.equipped." + wtype.name().toLowerCase()));
40-
}
41-
else
42-
{
43-
if (BackpackUtils.equipWearable(player.getCurrentEquippedItem(), player) == BackpackUtils.Reasons.SUCCESSFUL)
44-
{
45-
player.inventory.setInventorySlotContents(player.inventory.currentItem, null);
46-
player.inventoryContainer.detectAndSendChanges();
47-
}
48-
}
44+
player.inventory.setInventorySlotContents(player.inventory.currentItem, null);
45+
player.inventoryContainer.detectAndSendChanges();
4946
}
5047
}
51-
if (message.action == UNEQUIP_WEARABLE)
48+
else if (message.action == UNEQUIP_WEARABLE)
5249
{
5350
BackpackUtils.unequipWearable(player);
5451
}
@@ -60,10 +57,7 @@ public static class Message implements IMessage
6057
{
6158
private byte action;
6259

63-
public Message()
64-
{
65-
66-
}
60+
public Message() {}
6761

6862
public Message(byte action)
6963
{

0 commit comments

Comments
 (0)