Improved Networking system

This commit is contained in:
Timo Ley 2019-05-20 18:10:30 +02:00
parent b5349cfcc6
commit 99ffc6bf74
7 changed files with 32 additions and 15 deletions

View file

@ -2,12 +2,11 @@ package anvil.infinity;
import anvil.infinity.compat.CompatHandler;
import anvil.infinity.config.ConfigHandler;
import anvil.infinity.networking.KillAbilityPackageHandler;
import anvil.infinity.networking.KillAbilityReturnPackageHandler;
import anvil.infinity.networking.LookAbilityPackageHandler;
import anvil.infinity.networking.LookAbilityReturnPackageHandler;
import anvil.infinity.networking.PackageKillAbility;
import anvil.infinity.networking.PackageReq;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
@ -16,7 +15,6 @@ import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
@ -52,8 +50,8 @@ public class Infinity {
ConfigHandler.refreshConfig(event.getSuggestedConfigurationFile());
CompatHandler.check();
int netID = 0;
NETWORK_WRAPPER.registerMessage(KillAbilityReturnPackageHandler.class, PackageKillAbility.class, netID++, Side.SERVER);
NETWORK_WRAPPER.registerMessage(KillAbilityPackageHandler.class, PackageReq.class, netID++, Side.CLIENT);
NETWORK_WRAPPER.registerMessage(LookAbilityReturnPackageHandler.class, PackageKillAbility.class, netID++, Side.SERVER);
NETWORK_WRAPPER.registerMessage(LookAbilityPackageHandler.class, PackageReq.class, netID++, Side.CLIENT);
}

View file

@ -0,0 +1,4 @@
package anvil.infinity.helpers;
public class LokkAbilityHelper {
}

View file

@ -7,7 +7,7 @@ import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
public class KillAbilityPackageHandler implements IMessageHandler<PackageReq, IMessage> {
public class LookAbilityPackageHandler implements IMessageHandler<PackageReq, IMessage> {
@Override
public IMessage onMessage(PackageReq message, MessageContext ctx) {
@ -15,6 +15,7 @@ public class KillAbilityPackageHandler implements IMessageHandler<PackageReq, IM
if (result.typeOfHit == RayTraceResult.Type.ENTITY) {
PackageKillAbility pack = new PackageKillAbility();
pack.entity = result.entityHit;
pack.abilityID = message.id;
Infinity.NETWORK_WRAPPER.sendToServer(pack);
}

View file

@ -1,5 +1,6 @@
package anvil.infinity.networking;
import anvil.infinity.helpers.LookAbilityHelper;
import anvil.infinity.registry.Effects;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
@ -8,14 +9,11 @@ import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
public class KillAbilityReturnPackageHandler implements IMessageHandler<PackageKillAbility, IMessage> {
public class LookAbilityReturnPackageHandler implements IMessageHandler<PackageKillAbility, IMessage> {
@Override
public IMessage onMessage(PackageKillAbility message, MessageContext ctx) {
Entity e = message.entity;
if (e instanceof EntityLivingBase) {
((EntityLivingBase) e).addPotionEffect(new PotionEffect(Effects.snapEffect, 1));
}
LookAbilityHelper.activateEntityLookAbility(message.entity, message.abilityID);
return null;
}
}

View file

@ -10,6 +10,7 @@ import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
public class PackageKillAbility implements IMessage {
Entity entity;
public int abilityID;
public PackageKillAbility(Entity entity) {
super();
@ -28,6 +29,7 @@ public class PackageKillAbility implements IMessage {
String[] inputs = input.split(":");
int dim = Integer.parseInt(inputs[0]);
int id = Integer.parseInt(inputs[1]);
abilityID = Integer.parseInt(inputs[2]);
World world = FMLCommonHandler.instance().getMinecraftServerInstance().getWorld(dim);
entity = world.getEntityByID(id);
@ -36,7 +38,7 @@ public class PackageKillAbility implements IMessage {
@Override
public void toBytes(ByteBuf buf) {
ByteBufUtils.writeUTF8String(buf, entity.dimension + ":" + entity.getEntityId());
ByteBufUtils.writeUTF8String(buf, entity.dimension + ":" + entity.getEntityId() + ":" + abilityID);
}
}

View file

@ -1,17 +1,30 @@
package anvil.infinity.networking;
import anvil.infinity.helpers.LookAbilityHelper;
import io.netty.buffer.ByteBuf;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
public class PackageReq implements IMessage {
int id;
public PackageReq() {
id = LookAbilityHelper.KILL;
}
public PackageReq(int abilityID) {
id = abilityID;
}
@Override
public void fromBytes(ByteBuf buf) {
id = Integer.parseInt(ByteBufUtils.readUTF8String(buf));
}
@Override
public void toBytes(ByteBuf buf) {
ByteBufUtils.writeUTF8String(buf, "" + id);
}
}

View file

@ -14,6 +14,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionEffect;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.management.PlayerList;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumHand;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
@ -67,7 +68,7 @@ public class SnapHelper {
} else {
entity.setHeldItem(EnumHand.OFF_HAND, new ItemStack(ModuleInfinity.INFINITY_GAUNTLET));
}
entity.setHealth(1);
entity.attackEntityFrom(DamageSource.MAGIC, (entity.getHealth() / 10) - 0.01f);
} else if (data.selectedSnapResult == SnapResult.CREATIVE) {