Skip to content

Commit

Permalink
improve net code (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Glease authored Aug 3, 2023
1 parent cb6b675 commit b1135da
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 43 deletions.
57 changes: 36 additions & 21 deletions src/main/java/taintedmagic/common/items/tools/ItemKatana.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package taintedmagic.common.items.tools;

import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.WeakHashMap;

import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
Expand All @@ -28,10 +30,13 @@
import net.minecraftforge.client.event.RenderPlayerEvent;
import net.minecraftforge.common.MinecraftForge;

import org.apache.commons.lang3.tuple.MutablePair;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import taintedmagic.client.model.ModelKatana;
Expand Down Expand Up @@ -59,11 +64,8 @@ public class ItemKatana extends Item implements IWarpingGear, IRepairable {
public static final ModelKatana katana = new ModelKatana();
public static final ModelSaya saya = new ModelSaya();

public static boolean equipped = false;
public static float ticksEquipped = 0F;

public int ticksInUse = 0;

public ItemKatana() {
this.setCreativeTab(TaintedMagic.tabTaintedMagic);
this.setUnlocalizedName("ItemKatana");
Expand Down Expand Up @@ -169,7 +171,7 @@ public EnumRarity getRarity(ItemStack s) {
return EnumRarity.uncommon;
}

public float getAttackDamage(ItemStack s) {
public static float getAttackDamage(ItemStack s) {
switch (s.getItemDamage()) {
case 0:
return 14.25F;
Expand All @@ -196,8 +198,6 @@ public ItemStack onItemRightClick(ItemStack s, World w, EntityPlayer p) {
public void onUsingTick(ItemStack s, EntityPlayer p, int i) {
super.onUsingTick(s, p, i);

this.ticksInUse = getMaxItemUseDuration(s) - i;

float j = 1.0F + ((float) Math.random() * 0.25F);
if (p.ticksExisted % 5 == 0) p.worldObj.playSoundAtEntity(p, "thaumcraft:wind", j * 0.1F, j);
}
Expand All @@ -207,26 +207,27 @@ public void onPlayerStoppedUsing(ItemStack s, World w, EntityPlayer p, int i) {
super.onPlayerStoppedUsing(s, w, p, i);
Random r = new Random();

if (!hasAnyInscription(s) || !isFullyCharged(p) || p.isSneaking() || getInscription(s) == 2) {
boolean leech = (getInscription(s) == 2 && isFullyCharged(p));
boolean b = false;
int ticksInUse = getMaxItemUseDuration(s) - i;
boolean fullyCharged = ticksInUse >= 30;

if (!hasAnyInscription(s) || !fullyCharged || p.isSneaking() || getInscription(s) == 2) {

if (w.isRemote) {
MovingObjectPosition mop = Minecraft.getMinecraft().objectMouseOver;
float mul = Math.min(1.0F + (float) this.ticksInUse / 40.0F, 2.0F);

if (mop.entityHit != null) {
PacketHandler.INSTANCE.sendToServer(
new PacketKatanaAttack(mop.entityHit, p, this.getAttackDamage(s) * mul, leech));
PacketHandler.INSTANCE.sendToServer(new PacketKatanaAttack(mop.entityHit));
}
p.swingItem();
} else {
// 20 ticks to account for network lag
EventHandler.ticksInUse.put(p, MutablePair.of(20, ticksInUse));
}
p.worldObj.playSoundAtEntity(
p,
"thaumcraft:swing",
0.5F + (float) Math.random(),
0.5F + (float) Math.random());
} else if (hasAnyInscription(s) && isFullyCharged(p) && !p.isSneaking()) {
} else if (hasAnyInscription(s) && fullyCharged && !p.isSneaking()) {
switch (getInscription(s)) {
case 0: {
EntityExplosiveOrb proj = new EntityExplosiveOrb(w, p);
Expand Down Expand Up @@ -255,13 +256,6 @@ public void onPlayerStoppedUsing(ItemStack s, World w, EntityPlayer p, int i) {
}
}

private boolean isFullyCharged(EntityPlayer p) {
float f = Math.min((float) this.ticksInUse / 15.0F, 2.0F);

if (f == 2.0F) return true;
else return false;
}

@SideOnly(Side.CLIENT)
@SubscribeEvent
public void onPlayerRender(RenderPlayerEvent.Specials.Post event) {
Expand Down Expand Up @@ -422,4 +416,25 @@ public static ResourceLocation getTexture(ItemStack s) {
public int getWarp(ItemStack s, EntityPlayer p) {
return s.getItemDamage() == 0 ? 0 : s.getItemDamage() == 1 ? 3 : 7;
}

static {
FMLCommonHandler.instance().bus().register(new EventHandler());
}

public static class EventHandler {

public static WeakHashMap<EntityPlayer, MutablePair<Integer, Integer>> ticksInUse = new WeakHashMap<>();

@SubscribeEvent
public void onServerTick(TickEvent.ServerTickEvent e) {
if (e.phase == TickEvent.Phase.END) {
Iterator<MutablePair<Integer, Integer>> iter = ticksInUse.values().iterator();
while (iter.hasNext()) {
MutablePair<Integer, Integer> next = iter.next();
if (next.left > 0) next.left--;
else iter.remove();
}
}
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/taintedmagic/common/network/PacketHandler.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package taintedmagic.common.network;

import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import cpw.mods.fml.relauncher.Side;
import taintedmagic.common.lib.LibStrings;

public class PacketHandler {

static Marker SECURITY_MARKER = MarkerManager.getMarker("SuspiciousPackets");

public static final SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE
.newSimpleChannel(LibStrings.MODID.toLowerCase());

Expand Down
77 changes: 55 additions & 22 deletions src/main/java/taintedmagic/common/network/PacketKatanaAttack.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;

import org.apache.commons.lang3.tuple.MutablePair;

import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf;
import taintedmagic.common.TaintedMagic;
import taintedmagic.common.items.tools.ItemKatana;

public class PacketKatanaAttack implements IMessage, IMessageHandler<PacketKatanaAttack, IMessage> {

Expand All @@ -19,31 +23,61 @@ public class PacketKatanaAttack implements IMessage, IMessageHandler<PacketKatan
private int dimensionID;
private float dmg;
private boolean leech;
private boolean sus;

public PacketKatanaAttack() {}

public PacketKatanaAttack(Entity e, EntityPlayer p, float dmg, boolean leech) {
this.entityID = e.getEntityId();
this.playerID = p.getEntityId();
this.dimensionID = e.dimension;
this.dmg = dmg;
this.leech = leech;
public PacketKatanaAttack(final Entity entity) {
entityID = entity.getEntityId();
}

private static void log(EntityPlayerMP origin) {
TaintedMagic.log.warn(
PacketHandler.SECURITY_MARKER,
"Player {} tried to kill stuff around it",
origin.getGameProfile());
}

@Override
public IMessage onMessage(PacketKatanaAttack message, MessageContext ctx) {
World w = DimensionManager.getWorld(message.dimensionID);
if (w == null) return null;
public IMessage onMessage(final PacketKatanaAttack message, final MessageContext ctx) {
EntityPlayerMP realPlayer = ctx.getServerHandler().playerEntity;
ItemStack item = realPlayer.getCurrentEquippedItem();
if (item == null || !(item.getItem() instanceof ItemKatana)) {
log(realPlayer);
return null;
}
MutablePair<Integer, Integer> ticksInUseHolder = ItemKatana.EventHandler.ticksInUse.remove(realPlayer);
if (ticksInUseHolder == null) {
log(realPlayer);
return null;
}
int ticksInUse = ticksInUseHolder.right;
boolean fullyCharged = ticksInUse >= 30;
float damage = ItemKatana.getAttackDamage(item) * (1 + Math.min(ticksInUse / 40f, 1));
boolean leech = ItemKatana.getInscription(item) == 2 && fullyCharged;
if (sus && (realPlayer.getEntityId() != playerID || realPlayer.dimension != dimensionID
|| Math.abs(damage - message.dmg) > 1e-4
|| leech != message.leech)) {
log(realPlayer);
return null;
}
final World world = realPlayer.getEntityWorld();

final Entity entity = world.getEntityByID(message.entityID);

Entity e = w.getEntityByID(message.entityID);
Entity p = w.getEntityByID(message.playerID);
if (entity instanceof EntityLivingBase) {
double reach = realPlayer.theItemInWorldManager.getBlockReachDistance();
if (!(realPlayer.getDistanceSqToEntity(entity) < reach * reach)) {
log(realPlayer);
return null;
}

if (e != null && e instanceof EntityLivingBase && p != null && p instanceof EntityPlayer)
e.attackEntityFrom(DamageSource.causeIndirectMagicDamage(p, e), message.dmg);
entity.attackEntityFrom(DamageSource.causeIndirectMagicDamage(realPlayer, entity), damage);

if (message.leech && p instanceof EntityPlayer) {
((EntityPlayer) p).heal(message.dmg * 0.25F);
w.playSoundAtEntity(p, "thaumcraft:wand", 0.5F, 0.5F + ((float) Math.random() * 0.5F));
if (leech) {
realPlayer.heal(damage * 0.25F);
world.playSoundAtEntity(realPlayer, "thaumcraft:wand", 0.5F, 0.5F + ((float) Math.random() * 0.5F));
}
}

return null;
Expand All @@ -52,6 +86,9 @@ public IMessage onMessage(PacketKatanaAttack message, MessageContext ctx) {
@Override
public void fromBytes(ByteBuf buf) {
this.entityID = buf.readInt();
if (buf.readableBytes() == 0) return;
// forged packets, but could also be an outdated client
this.sus = true;
this.playerID = buf.readInt();
this.dimensionID = buf.readInt();
this.dmg = buf.readFloat();
Expand All @@ -61,9 +98,5 @@ public void fromBytes(ByteBuf buf) {
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(this.entityID);
buf.writeInt(this.playerID);
buf.writeInt(this.dimensionID);
buf.writeFloat(this.dmg);
buf.writeBoolean(this.leech);
}
}

0 comments on commit b1135da

Please sign in to comment.