Skip to content

Commit

Permalink
Fix syncing when players are tracked
Browse files Browse the repository at this point in the history
Remove superfluous sending of sync packets
  • Loading branch information
pau101 committed Dec 12, 2017
1 parent 7035b13 commit 2255c9c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 50 deletions.
115 changes: 69 additions & 46 deletions src/main/java/baubles/common/event/EventHandlerEntity.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
package baubles.common.event;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import baubles.api.cap.BaublesCapabilities;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.WorldServer;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.player.PlayerDropsEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedOutEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import baubles.api.BaublesApi;
import baubles.api.IBauble;
import baubles.api.cap.BaublesContainer;
Expand Down Expand Up @@ -55,20 +62,18 @@ public void attachCapabilitiesPlayer(AttachCapabilitiesEvent<Entity> event) {

@SubscribeEvent
public void playerJoin(EntityJoinWorldEvent event) {
if (event.getEntity() instanceof EntityPlayer && !event.getWorld().isRemote) {
IBaublesItemHandler baubles = BaublesApi.getBaublesHandler((EntityPlayer) event.getEntity());

for (int a=0;a<baubles.getSlots();a++) baubles.setChanged(a,true);
Entity entity = event.getEntity();
if (entity instanceof EntityPlayerMP) {
EntityPlayerMP player = (EntityPlayerMP) entity;
syncSlots(player, Collections.singletonList(player));
}
}

for (EntityPlayer p:event.getEntity().getEntityWorld().playerEntities) {
if (p.getEntityId() != event.getEntity().getEntityId()) {
IBaublesItemHandler baubles2 = BaublesApi.getBaublesHandler(p);
for (int a=0;a<baubles2.getSlots();a++) baubles2.setChanged(a,true);
}
}
ItemStack[] sl = new ItemStack[baubles.getSlots()];
Arrays.fill(sl, ItemStack.EMPTY);
baublesSync.put(event.getEntity().getUniqueID(), sl);
@SubscribeEvent
public void onStartTracking(PlayerEvent.StartTracking event) {
Entity target = event.getTarget();
if (target instanceof EntityPlayerMP) {
syncSlots((EntityPlayer) target, Collections.singletonList(event.getEntityPlayer()));
}
}

Expand All @@ -79,49 +84,67 @@ public void onPlayerLoggedOut(PlayerLoggedOutEvent event)
}

@SubscribeEvent
public void playerTick(PlayerEvent.LivingUpdateEvent event) {
public void playerTick(TickEvent.PlayerTickEvent event) {
// player events
if (event.getEntity() instanceof EntityPlayer) {
EntityPlayer player = (EntityPlayer) event.getEntity();
if (event.phase == TickEvent.Phase.END) {
EntityPlayer player = event.player;
IBaublesItemHandler baubles = BaublesApi.getBaublesHandler(player);
ItemStack[] items = baublesSync.get(player.getUniqueID());
if (items==null) {
ItemStack[] sl = new ItemStack[baubles.getSlots()];
Arrays.fill(sl, ItemStack.EMPTY);
baublesSync.put(player.getUniqueID(), sl);
items = baublesSync.get(player.getUniqueID());
}
int count = baubles.getSlots();
if(items.length != count) {
ItemStack[] old = items;
items = new ItemStack[count];
for(int i = 0;i<old.length && i<items.length;i++) {
items[i] = old[i];
for (int i = 0; i < baubles.getSlots(); i++) {
ItemStack stack = baubles.getStackInSlot(i);
IBauble bauble = stack.getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null);
if (bauble != null) {
bauble.onWornTick(stack, player);
}
baublesSync.put(player.getUniqueID(), items);
}
if (!player.world.isRemote) {
syncBaubles(player, baubles);
}
}
}

for (int a = 0; a < count; a++) {
ItemStack baubleStack = baubles.getStackInSlot(a);
IBauble bauble = null;
if (!baubleStack.isEmpty() && baubleStack.hasCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null)) {
bauble = baubleStack.getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null);
//Worn Tick
bauble.onWornTick(baubleStack, player);
}
//Sync
if (!player.getEntityWorld().isRemote) {
if(baubles.isChanged(a) || (bauble!=null && bauble.willAutoSync(baubleStack, player) && !ItemStack.areItemStacksEqual(baubleStack, items[a]))) {
try {
PacketHandler.INSTANCE.sendToDimension(new PacketSync(player, a), player.getEntityWorld().provider.getDimension());
} catch (Exception e) { }
items[a] = baubleStack != null ? baubleStack.copy() : ItemStack.EMPTY;
}
private void syncBaubles(EntityPlayer player, IBaublesItemHandler baubles) {
ItemStack[] items = baublesSync.get(player.getUniqueID());
if (items == null) {
items = new ItemStack[baubles.getSlots()];
Arrays.fill(items, ItemStack.EMPTY);
baublesSync.put(player.getUniqueID(), items);
}
if (items.length != baubles.getSlots()) {
ItemStack[] old = items;
items = new ItemStack[baubles.getSlots()];
System.arraycopy(old, 0, items, 0, Math.min(old.length, items.length));
baublesSync.put(player.getUniqueID(), items);
}
Set<EntityPlayer> receivers = null;
for (int i = 0; i < baubles.getSlots(); i++) {
ItemStack stack = baubles.getStackInSlot(i);
IBauble bauble = stack.getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null);
if (baubles.isChanged(i) || bauble != null && bauble.willAutoSync(stack, player) && !ItemStack.areItemStacksEqual(stack, items[i])) {
if (receivers == null) {
receivers = new HashSet<>(((WorldServer) player.world).getEntityTracker().getTrackingPlayers(player));
receivers.add(player);
}
syncSlot(player, i, stack, receivers);
baubles.setChanged(i,false);
items[i] = stack == null ? ItemStack.EMPTY : stack.copy();
}
}
}

private void syncSlots(EntityPlayer player, Collection<? extends EntityPlayer> receivers) {
IBaublesItemHandler baubles = BaublesApi.getBaublesHandler(player);
for (int i = 0; i < baubles.getSlots(); i++) {
syncSlot(player, i, baubles.getStackInSlot(i), receivers);
}
}

private void syncSlot(EntityPlayer player, int slot, ItemStack stack, Collection<? extends EntityPlayer> receivers) {
PacketSync pkt = new PacketSync(player, slot, stack);
for (EntityPlayer receiver : receivers) {
PacketHandler.INSTANCE.sendTo(pkt, (EntityPlayerMP) receiver);
}
}

@SubscribeEvent
public void playerDeath(PlayerDropsEvent event) {
if (event.getEntity() instanceof EntityPlayer
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/baubles/common/network/PacketSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ public class PacketSync implements IMessage {

public PacketSync() {}

public PacketSync(EntityPlayer p, int slot) {
IBaublesItemHandler baubles = BaublesApi.getBaublesHandler(p);
public PacketSync(EntityPlayer p, int slot, ItemStack bauble) {
this.slot = (byte) slot;
this.bauble = baubles.getStackInSlot(slot);
this.bauble = bauble;
this.playerId = p.getEntityId();
baubles.setChanged(slot,false);
}

@Override
Expand Down

0 comments on commit 2255c9c

Please sign in to comment.