Handle nulls in EntityRewriter (#11)

This commit is contained in:
Matsv 2017-06-21 19:30:48 +02:00
parent 0e5ee7063f
commit a5aa9e7281
No known key found for this signature in database
GPG Key ID: 97CEC2A2EA31350F
3 changed files with 17 additions and 7 deletions

View File

@ -19,6 +19,7 @@ import us.myles.ViaVersion.api.data.StoredObject;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
public class EntityTracker extends StoredObject { public class EntityTracker extends StoredObject {
@ -51,12 +52,12 @@ public class EntityTracker extends StoredObject {
public AbstractEntityType getEntityType(int id) { public AbstractEntityType getEntityType(int id) {
if (containsEntity(id)) if (containsEntity(id))
return getEntity(id).getType(); return getEntity(id).get().getType();
return null; return null;
} }
public StoredEntity getEntity(int id) { public Optional<StoredEntity> getEntity(int id) {
return entityMap.get(id); return Optional.ofNullable(entityMap.get(id));
} }
public boolean containsEntity(int id) { public boolean containsEntity(int id) {

View File

@ -29,6 +29,7 @@ import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.minecraft.metadata.MetaType; import us.myles.ViaVersion.api.minecraft.metadata.MetaType;
import us.myles.ViaVersion.api.minecraft.metadata.Metadata; import us.myles.ViaVersion.api.minecraft.metadata.Metadata;
import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_9; import us.myles.ViaVersion.api.minecraft.metadata.types.MetaType1_9;
import us.myles.ViaVersion.exception.CancelException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -98,7 +99,13 @@ public abstract class EntityRewriter<T extends BackwardsProtocol> extends Rewrit
} }
protected MetaStorage handleMeta(UserConnection user, int entityId, MetaStorage storage) throws Exception { protected MetaStorage handleMeta(UserConnection user, int entityId, MetaStorage storage) throws Exception {
EntityTracker.StoredEntity entity = getEntityTracker(user).getEntity(entityId); Optional<EntityTracker.StoredEntity> optEntity = getEntityTracker(user).getEntity(entityId);
if (!optEntity.isPresent()) {
ViaBackwards.getPlatform().getLogger().warning("Metadata for entity id: " + entityId + " not sent because the entity doesn't exist. " + storage);
throw new CancelException();
}
EntityTracker.StoredEntity entity = optEntity.get();
AbstractEntityType type = entity.getType(); AbstractEntityType type = entity.getType();
List<Metadata> newList = new CopyOnWriteArrayList<>(); List<Metadata> newList = new CopyOnWriteArrayList<>();

View File

@ -428,7 +428,8 @@ public class BlockItemPackets1_11 extends BlockItemRewriter<Protocol1_10To1_11>
WindowTracker tracker = user.get(WindowTracker.class); WindowTracker tracker = user.get(WindowTracker.class);
if (tracker.getInventory() != null && tracker.getInventory().equals("EntityHorse")) { if (tracker.getInventory() != null && tracker.getInventory().equals("EntityHorse")) {
EntityTracker.ProtocolEntityTracker entTracker = user.get(EntityTracker.class).get(getProtocol()); EntityTracker.ProtocolEntityTracker entTracker = user.get(EntityTracker.class).get(getProtocol());
if (tracker.getEntityId() != -1 && entTracker.getEntity(tracker.getEntityId()).getType().is(EntityType1_11.EntityType.LIAMA)) Optional<EntityTracker.StoredEntity> optEntity = entTracker.getEntity(tracker.getEntityId());
if (optEntity.isPresent() && optEntity.get().getType().is(EntityType1_11.EntityType.LIAMA))
return true; return true;
} }
return false; return false;
@ -438,8 +439,9 @@ public class BlockItemPackets1_11 extends BlockItemRewriter<Protocol1_10To1_11>
WindowTracker tracker = user.get(WindowTracker.class); WindowTracker tracker = user.get(WindowTracker.class);
if (tracker.getInventory() != null && tracker.getInventory().equals("EntityHorse")) { if (tracker.getInventory() != null && tracker.getInventory().equals("EntityHorse")) {
EntityTracker.ProtocolEntityTracker entTracker = user.get(EntityTracker.class).get(getProtocol()); EntityTracker.ProtocolEntityTracker entTracker = user.get(EntityTracker.class).get(getProtocol());
if (tracker.getEntityId() != -1) Optional<EntityTracker.StoredEntity> optEntity = entTracker.getEntity(tracker.getEntityId());
return Optional.of(entTracker.getEntity(tracker.getEntityId()).get(ChestedHorseStorage.class)); if (optEntity.isPresent())
return Optional.of(optEntity.get().get(ChestedHorseStorage.class));
} }
return Optional.empty(); return Optional.empty();
} }