Player effect cache cleanup

Only cache effects and not their values unless we actually use the value.
This commit is contained in:
Camotoy 2021-08-16 11:53:56 -04:00
parent ce748990a4
commit ac17963baa
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
5 changed files with 352 additions and 322 deletions

View file

@ -26,29 +26,53 @@
package org.geysermc.connector.network.session.cache;
import com.github.steveice10.mc.protocol.data.game.entity.Effect;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import lombok.Getter;
import java.util.EnumSet;
import java.util.Set;
public class EntityEffectCache {
/**
* Used to clear effects on dimension switch.
*/
@Getter
private final Object2IntMap<Effect> entityEffects = new Object2IntOpenHashMap<>();
private final Set<Effect> entityEffects = EnumSet.noneOf(Effect.class);
public void addEffect(Effect effect, int effectAmplifier) {
if (effect != null) {
entityEffects.putIfAbsent(effect, effectAmplifier + 1);
/* Used to track mining speed */
@Getter
private int conduitPower;
@Getter
private int haste;
@Getter
private int miningFatigue;
public void setEffect(Effect effect, int effectAmplifier) {
switch (effect) {
case CONDUIT_POWER:
conduitPower = effectAmplifier + 1;
break;
case FASTER_DIG:
haste = effectAmplifier + 1;
break;
case SLOWER_DIG:
miningFatigue = effectAmplifier + 1;
break;
}
entityEffects.add(effect);
}
public void removeEffect(Effect effect) {
if (entityEffects.containsKey(effect)) {
int effectLevel = entityEffects.getInt(effect);
entityEffects.remove(effect, effectLevel);
switch (effect) {
case CONDUIT_POWER:
conduitPower = 0;
break;
case FASTER_DIG:
haste = 0;
break;
case SLOWER_DIG:
miningFatigue = 0;
break;
}
}
public int getEffectLevel(Effect effect) {
return entityEffects.getOrDefault(effect, 0);
entityEffects.remove(effect);
}
}

View file

@ -38,10 +38,12 @@ public class JavaEntityEffectTranslator extends PacketTranslator<ServerEntityEff
@Override
public void translate(ServerEntityEffectPacket packet, GeyserSession session) {
Entity entity = session.getEntityCache().getEntityByJavaId(packet.getEntityId());
Entity entity;
if (packet.getEntityId() == session.getPlayerEntity().getEntityId()) {
entity = session.getPlayerEntity();
session.getEffectCache().addEffect(packet.getEffect(), packet.getAmplifier());
session.getEffectCache().setEffect(packet.getEffect(), packet.getAmplifier());
} else {
entity = session.getEntityCache().getEntityByJavaId(packet.getEntityId());
}
if (entity == null)
return;

View file

@ -38,10 +38,12 @@ public class JavaEntityRemoveEffectTranslator extends PacketTranslator<ServerEnt
@Override
public void translate(ServerEntityRemoveEffectPacket packet, GeyserSession session) {
Entity entity = session.getEntityCache().getEntityByJavaId(packet.getEntityId());
Entity entity;
if (packet.getEntityId() == session.getPlayerEntity().getEntityId()) {
entity = session.getPlayerEntity();
session.getEffectCache().removeEffect(packet.getEffect());
} else {
entity = session.getEntityCache().getEntityByJavaId(packet.getEntityId());
}
if (entity == null)
return;

View file

@ -25,7 +25,6 @@
package org.geysermc.connector.utils;
import com.github.steveice10.mc.protocol.data.game.entity.Effect;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.nukkitx.math.vector.Vector3i;
@ -171,8 +170,8 @@ public class BlockUtils {
false, false);
}
hasteLevel = Math.max(session.getEffectCache().getEffectLevel(Effect.FASTER_DIG), session.getEffectCache().getEffectLevel(Effect.CONDUIT_POWER));
miningFatigueLevel = session.getEffectCache().getEffectLevel(Effect.SLOWER_DIG);
hasteLevel = Math.max(session.getEffectCache().getHaste(), session.getEffectCache().getConduitPower());
miningFatigueLevel = session.getEffectCache().getMiningFatigue();
boolean isInWater = session.getCollisionManager().isPlayerInWater();

View file

@ -28,7 +28,7 @@ package org.geysermc.connector.utils;
import com.github.steveice10.mc.protocol.data.game.entity.Effect;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.packet.ChangeDimensionPacket;
import com.nukkitx.protocol.bedrock.packet.MobEffectPacket;
import com.nukkitx.protocol.bedrock.packet.StopSoundPacket;
@ -36,6 +36,8 @@ import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.entity.Entity;
import org.geysermc.connector.network.session.GeyserSession;
import java.util.Set;
public class DimensionUtils {
// Changes if the above-bedrock Nether building workaround is applied
@ -63,27 +65,28 @@ public class DimensionUtils {
session.getLecternCache().clear();
session.getSkullCache().clear();
Vector3i pos = Vector3i.from(0, Short.MAX_VALUE, 0);
Vector3f pos = Vector3f.from(0, Short.MAX_VALUE, 0);
ChangeDimensionPacket changeDimensionPacket = new ChangeDimensionPacket();
changeDimensionPacket.setDimension(bedrockDimension);
changeDimensionPacket.setRespawn(true);
changeDimensionPacket.setPosition(pos.toFloat());
changeDimensionPacket.setPosition(pos);
session.sendUpstreamPacket(changeDimensionPacket);
session.setDimension(javaDimension);
player.setPosition(pos.toFloat());
player.setPosition(pos);
session.setSpawned(false);
session.setLastChunkPosition(null);
for (Effect effect : session.getEffectCache().getEntityEffects().keySet()) {
Set<Effect> entityEffects = session.getEffectCache().getEntityEffects();
for (Effect effect : entityEffects) {
MobEffectPacket mobEffectPacket = new MobEffectPacket();
mobEffectPacket.setEvent(MobEffectPacket.Event.REMOVE);
mobEffectPacket.setRuntimeEntityId(session.getPlayerEntity().getGeyserId());
mobEffectPacket.setRuntimeEntityId(player.getGeyserId());
mobEffectPacket.setEffectId(EntityUtils.toBedrockEffectId(effect));
session.sendUpstreamPacket(mobEffectPacket);
}
// Effects are re-sent from server
session.getEffectCache().getEntityEffects().clear();
entityEffects.clear();
//let java server handle portal travel sound
StopSoundPacket stopSoundPacket = new StopSoundPacket();