TurnBasedMinecraftMod/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/networking/PacketBattleMessage.java

303 lines
11 KiB
Java
Raw Normal View History

2019-11-25 06:29:25 +00:00
package com.burnedkirby.TurnBasedMinecraft.common.networking;
2018-09-06 08:08:36 +00:00
import java.util.HashMap;
import java.util.Map;
2019-10-21 07:13:11 +00:00
import java.util.function.Supplier;
2018-09-06 08:08:36 +00:00
2019-11-25 06:29:25 +00:00
import com.burnedkirby.TurnBasedMinecraft.common.TurnBasedMinecraftMod;
2018-09-06 08:08:36 +00:00
import com.burnedkirby.TurnBasedMinecraft.common.Utility;
2018-09-06 08:08:36 +00:00
import net.minecraft.entity.Entity;
2019-10-21 07:13:11 +00:00
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.dimension.DimensionType;
2019-10-21 07:13:11 +00:00
import net.minecraftforge.fml.network.NetworkEvent;
2018-09-06 08:08:36 +00:00
2019-10-21 07:13:11 +00:00
public class PacketBattleMessage
2018-09-06 08:08:36 +00:00
{
public enum MessageType
{
ENTERED(0),
FLEE(1),
DIED(2),
ENDED(3),
ATTACK(4),
DEFEND(5),
DEFENSE_DAMAGE(6),
MISS(7),
DEFENDING(8),
DID_NOTHING(9),
2018-09-10 05:59:56 +00:00
USED_ITEM(10),
TURN_BEGIN(11),
TURN_END(12),
SWITCHED_ITEM(13),
WAS_AFFECTED(14),
BECAME_CREATIVE(15),
FIRED_ARROW(16),
ARROW_HIT(17),
BOW_NO_AMMO(18);
2018-09-06 08:08:36 +00:00
private int value;
private static Map<Integer, MessageType> map = new HashMap<Integer, MessageType>();
MessageType(int value)
2018-09-06 08:08:36 +00:00
{
this.value = value;
}
public int getValue()
{
return value;
}
static
{
for(MessageType type : MessageType.values())
{
map.put(type.getValue(), type);
}
}
public static MessageType valueOf(int value)
{
return map.get(value);
}
}
public enum UsedItemAction
{
USED_NOTHING(0),
USED_INVALID(1),
USED_FOOD(2),
USED_POTION(3);
private int value;
private static Map<Integer, UsedItemAction> map = new HashMap<Integer, UsedItemAction>();
UsedItemAction(int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
static
{
for(UsedItemAction type : UsedItemAction.values())
{
map.put(type.getValue(), type);
}
}
public static UsedItemAction valueOf(int value)
{
return map.get(value);
}
}
2018-09-06 08:08:36 +00:00
MessageType messageType;
int entityIDFrom;
int entityIDTo;
int amount;
String custom;
DimensionType dimension;
2018-09-06 08:08:36 +00:00
public PacketBattleMessage() { custom = new String(); }
2018-09-06 08:08:36 +00:00
public PacketBattleMessage(MessageType messageType, int entityIDFrom, int entityIDTo, DimensionType dimension, int amount)
2018-09-06 08:08:36 +00:00
{
this.messageType = messageType;
this.entityIDFrom = entityIDFrom;
this.entityIDTo = entityIDTo;
this.dimension = dimension;
2018-09-06 08:08:36 +00:00
this.amount = amount;
custom = new String();
}
public PacketBattleMessage(MessageType messageType, int entityIDFrom, int entityIDTo, DimensionType dimension, int amount, String custom)
{
this.messageType = messageType;
this.entityIDFrom = entityIDFrom;
this.entityIDTo = entityIDTo;
this.dimension = dimension;
this.amount = amount;
this.custom = custom;
2018-09-06 08:08:36 +00:00
}
2019-10-21 07:13:11 +00:00
public static void encode(PacketBattleMessage pkt, PacketBuffer buf) {
buf.writeInt(pkt.messageType.getValue());
buf.writeInt(pkt.entityIDFrom);
buf.writeInt(pkt.entityIDTo);
buf.writeString(DimensionType.getKey(pkt.dimension).toString());
2019-10-21 07:13:11 +00:00
buf.writeInt(pkt.amount);
buf.writeString(pkt.custom);
2018-09-06 08:08:36 +00:00
}
2019-10-21 07:13:11 +00:00
public static PacketBattleMessage decode(PacketBuffer buf) {
return new PacketBattleMessage(
MessageType.valueOf(
buf.readInt()),
buf.readInt(),
buf.readInt(),
DimensionType.byName(new ResourceLocation(buf.readString())),
2019-10-21 07:13:11 +00:00
buf.readInt(),
buf.readString());
2018-09-06 08:08:36 +00:00
}
2019-10-21 07:13:11 +00:00
public static class Handler {
public static void handle(final PacketBattleMessage pkt, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
Entity fromEntity = TurnBasedMinecraftMod.proxy.getEntity(pkt.entityIDFrom, pkt.dimension);
2019-10-21 07:13:11 +00:00
String from = "Unknown";
if(fromEntity != null)
2018-09-06 08:08:36 +00:00
{
2019-10-21 07:13:11 +00:00
from = fromEntity.getDisplayName().getFormattedText();
2018-09-06 08:08:36 +00:00
}
2019-10-21 07:13:11 +00:00
else if(TurnBasedMinecraftMod.proxy.getLocalBattle() != null)
2018-09-06 08:08:36 +00:00
{
2019-10-21 07:13:11 +00:00
fromEntity = TurnBasedMinecraftMod.proxy.getLocalBattle().getCombatantEntity(pkt.entityIDFrom);
if(fromEntity != null)
2018-09-06 08:08:36 +00:00
{
2019-10-21 07:13:11 +00:00
from = fromEntity.getDisplayName().getFormattedText();
2018-09-06 08:08:36 +00:00
}
}
Entity toEntity = TurnBasedMinecraftMod.proxy.getEntity(pkt.entityIDTo, pkt.dimension);
2019-10-21 07:13:11 +00:00
String to = "Unknown";
if(toEntity != null)
2018-09-06 08:08:36 +00:00
{
2019-10-21 07:13:11 +00:00
to = toEntity.getDisplayName().getFormattedText();
2018-09-06 08:08:36 +00:00
}
2019-10-21 07:13:11 +00:00
else if(TurnBasedMinecraftMod.proxy.getLocalBattle() != null)
2018-09-06 08:08:36 +00:00
{
2019-10-21 07:13:11 +00:00
toEntity = TurnBasedMinecraftMod.proxy.getLocalBattle().getCombatantEntity(pkt.entityIDTo);
if(toEntity != null)
{
to = toEntity.getDisplayName().getFormattedText();
}
2018-09-06 08:08:36 +00:00
}
2019-10-21 07:13:11 +00:00
switch(pkt.messageType)
2018-09-06 08:08:36 +00:00
{
2019-10-21 07:13:11 +00:00
case ENTERED:
TurnBasedMinecraftMod.proxy.displayString(from + " entered battle!");
if(TurnBasedMinecraftMod.proxy.getLocalBattle() == null || TurnBasedMinecraftMod.proxy.getLocalBattle().getId() != pkt.amount)
2018-09-06 08:08:36 +00:00
{
2019-10-21 07:13:11 +00:00
TurnBasedMinecraftMod.proxy.createLocalBattle(pkt.amount);
2018-09-06 08:08:36 +00:00
}
2019-10-21 07:13:11 +00:00
TurnBasedMinecraftMod.proxy.battleStarted();
TurnBasedMinecraftMod.proxy.typeEnteredBattle(pkt.custom);
break;
case FLEE:
if(pkt.amount != 0)
2018-09-06 08:08:36 +00:00
{
2019-10-21 07:13:11 +00:00
TurnBasedMinecraftMod.proxy.displayString(from + " fled battle!");
TurnBasedMinecraftMod.proxy.typeLeftBattle(pkt.custom);
2018-09-06 08:08:36 +00:00
}
else
{
2019-10-21 07:13:11 +00:00
TurnBasedMinecraftMod.proxy.displayString(from + " tried to flee battle but failed!");
2018-09-06 08:08:36 +00:00
}
break;
2019-10-21 07:13:11 +00:00
case DIED:
TurnBasedMinecraftMod.proxy.displayString(from + " died in battle!");
TurnBasedMinecraftMod.proxy.typeLeftBattle(pkt.custom);
break;
case ENDED:
TurnBasedMinecraftMod.proxy.displayString("Battle has ended!");
TurnBasedMinecraftMod.proxy.battleEnded();
break;
case ATTACK:
TurnBasedMinecraftMod.proxy.displayString(from + " attacked " + to + " and dealt " + pkt.amount + " damage!");
break;
case DEFEND:
TurnBasedMinecraftMod.proxy.displayString(from + " blocked " + to + "'s attack!");
break;
case DEFENSE_DAMAGE:
TurnBasedMinecraftMod.proxy.displayString(from + " retaliated from " + to + "'s attack and dealt " + pkt.amount + " damage!");
break;
case MISS:
TurnBasedMinecraftMod.proxy.displayString(from + " attacked " + to + " but missed!");
break;
case DEFENDING:
TurnBasedMinecraftMod.proxy.displayString(from + " is defending!");
break;
case DID_NOTHING:
TurnBasedMinecraftMod.proxy.displayString(from + " did nothing!");
break;
case USED_ITEM:
switch(UsedItemAction.valueOf(pkt.amount))
2018-09-11 07:39:46 +00:00
{
2019-10-21 07:13:11 +00:00
case USED_NOTHING:
TurnBasedMinecraftMod.proxy.displayString(from + " tried to use nothing!");
break;
case USED_INVALID:
if(pkt.custom.length() > 0)
{
TurnBasedMinecraftMod.proxy.displayString(from + " tried to consume " + pkt.custom + " and failed!");
}
else
{
TurnBasedMinecraftMod.proxy.displayString(from + " tried to consume an invalid item and failed!");
}
break;
case USED_FOOD:
TurnBasedMinecraftMod.proxy.displayString(from + " ate a " + pkt.custom + "!");
break;
case USED_POTION:
TurnBasedMinecraftMod.proxy.displayString(from + " drank a " + pkt.custom + "!");
break;
2018-09-11 07:39:46 +00:00
}
break;
2019-10-21 07:13:11 +00:00
case TURN_BEGIN:
TurnBasedMinecraftMod.proxy.displayString("The turn begins!");
TurnBasedMinecraftMod.proxy.battleGuiTurnBegin();
break;
2019-10-21 07:13:11 +00:00
case TURN_END:
if(TurnBasedMinecraftMod.proxy.getLocalBattle() != null)
{
if(pkt.amount == 0)
{
TurnBasedMinecraftMod.proxy.displayString("The turn ended!");
}
else
{
TurnBasedMinecraftMod.proxy.displayString("The turn ended (abnormally due to internal error)!");
}
}
TurnBasedMinecraftMod.proxy.battleGuiTurnEnd();
break;
2019-10-21 07:13:11 +00:00
case SWITCHED_ITEM:
if(pkt.amount != 0)
2018-10-29 05:46:45 +00:00
{
2019-10-21 07:13:11 +00:00
TurnBasedMinecraftMod.proxy.displayString(from + " switched to a different item!");
2018-10-29 05:46:45 +00:00
}
else
{
2019-10-21 07:13:11 +00:00
TurnBasedMinecraftMod.proxy.displayString(from + " switched to a different item but failed because it was invalid!");
2018-10-29 05:46:45 +00:00
}
2019-10-21 07:13:11 +00:00
break;
case WAS_AFFECTED:
TurnBasedMinecraftMod.proxy.displayString(to + " was " + pkt.custom + " by " + from + "!");
break;
case BECAME_CREATIVE:
TurnBasedMinecraftMod.proxy.displayString(from + " entered creative mode and left battle!");
break;
case FIRED_ARROW:
TurnBasedMinecraftMod.proxy.displayString(from + " let loose an arrow towards " + to + "!");
break;
case ARROW_HIT:
TurnBasedMinecraftMod.proxy.displayString(to + " was hit by " + from + "'s arrow!");
break;
case BOW_NO_AMMO:
TurnBasedMinecraftMod.proxy.displayString(from + " tried to use their bow but ran out of ammo!");
break;
}
2019-10-21 07:13:11 +00:00
});
ctx.get().setPacketHandled(true);
}
2018-09-06 08:08:36 +00:00
}
}