Fix several NPE cases, apply final to relevant fields, remove unused code

The suggested changes were found with FindBugs, some of these bugs may be rare cases.
This commit is contained in:
Myles 2019-05-27 17:50:08 +01:00
parent 3027490256
commit 744fa25349
24 changed files with 90 additions and 84 deletions

View File

@ -79,7 +79,8 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform {
// Check if it's a spigot build with a protocol mod
try {
compatSpigotBuild = NMSUtil.nms("PacketEncoder").getDeclaredField("version") != null;
NMSUtil.nms("PacketEncoder").getDeclaredField("version");
compatSpigotBuild = true;
} catch (Exception e) {
compatSpigotBuild = false;
}

View File

@ -191,7 +191,7 @@ public abstract class Protocol {
@AllArgsConstructor
@Getter
class ProtocolPacket {
public static class ProtocolPacket {
State state;
int oldID;
int newID;

View File

@ -27,6 +27,6 @@ public class ShortType extends Type<Short> implements TypeConverter<Short> {
if (o instanceof Boolean) {
return ((Boolean) o) ? (short) 1 : 0;
}
return (short) o;
return (Short) o;
}
}

View File

@ -27,6 +27,6 @@ public class UnsignedByteType extends Type<Short> implements TypeConverter<Short
if (o instanceof Boolean) {
return ((Boolean) o) ? (short) 1 : 0;
}
return (short) o;
return (Short) o;
}
}

View File

@ -87,12 +87,13 @@ public class Protocol1_11To1_10 extends Protocol {
int type = wrapper.get(Type.VAR_INT, 1);
Entity1_11Types.EntityType entType = MetadataRewriter1_11To1_10.rewriteEntityType(type, wrapper.get(Types1_9.METADATA_LIST, 0));
if (entType != null)
if (entType != null) {
wrapper.set(Type.VAR_INT, 1, entType.getId());
// Register Type ID
wrapper.user().get(EntityTracker1_11.class).addEntity(entityId, entType);
get(MetadataRewriter1_11To1_10.class).handleMetadata(entityId, entType, wrapper.get(Types1_9.METADATA_LIST, 0), wrapper.user());
// Register Type ID
wrapper.user().get(EntityTracker1_11.class).addEntity(entityId, entType);
get(MetadataRewriter1_11To1_10.class).handleMetadata(entityId, entType, wrapper.get(Types1_9.METADATA_LIST, 0), wrapper.user());
}
}
});
}

View File

@ -45,9 +45,9 @@ public class EntityPackets {
int data = wrapper.get(Type.INT, 0);
wrapper.set(Type.INT, 0, Protocol1_13_1To1_13.getNewBlockStateId(data));
}
// Register Type ID
wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType);
}
// Register Type ID
wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType);
}
});
}

View File

@ -104,9 +104,9 @@ public class WorldPackets {
int id = wrapper.get(Type.INT, 0);
int data = wrapper.get(Type.INT, 1);
if (id == 1010) { // Play record
wrapper.set(Type.INT, 1, data = InventoryPackets.getNewItemId(data));
wrapper.set(Type.INT, 1, InventoryPackets.getNewItemId(data));
} else if (id == 2001) { // Block break + block break sound
wrapper.set(Type.INT, 1, data = Protocol1_13_1To1_13.getNewBlockStateId(data));
wrapper.set(Type.INT, 1, Protocol1_13_1To1_13.getNewBlockStateId(data));
}
}
});

View File

@ -135,7 +135,9 @@ public class ChatRewriter {
String oldTranslate = ((TranslatableComponent) component).getTranslate();
String newTranslate;
newTranslate = MappingData.translateMapping.get(oldTranslate);
if (newTranslate == null) MappingData.mojangTranslation.get(oldTranslate);
if (newTranslate == null) {
newTranslate = MappingData.mojangTranslation.get(oldTranslate);
}
if (newTranslate != null) {
((TranslatableComponent) component).setTranslate(newTranslate);
}

View File

@ -40,7 +40,6 @@ import us.myles.ViaVersion.util.GsonUtil;
import java.util.EnumMap;
import java.util.Map;
// Development of 1.13 support!
public class Protocol1_13To1_12_2 extends Protocol {
public static final Particle1_13Type PARTICLE_TYPE = new Particle1_13Type();
@ -106,8 +105,7 @@ public class Protocol1_13To1_12_2 extends Protocol {
};
// These are arbitrary rewrite values, it just needs an invalid color code character.
protected static EnumMap<ChatColor, Character> SCOREBOARD_TEAM_NAME_REWRITE = new EnumMap<>(ChatColor.class);
// @formatter:on
protected final static EnumMap<ChatColor, Character> SCOREBOARD_TEAM_NAME_REWRITE = new EnumMap<>(ChatColor.class);
static {
SCOREBOARD_TEAM_NAME_REWRITE.put(ChatColor.BLACK, 'g');
@ -363,11 +361,11 @@ public class Protocol1_13To1_12_2 extends Protocol {
int id = wrapper.get(Type.INT, 0);
int data = wrapper.get(Type.INT, 1);
if (id == 1010) { // Play record
wrapper.set(Type.INT, 1, data = MappingData.oldToNewItems.get(data << 4));
wrapper.set(Type.INT, 1, MappingData.oldToNewItems.get(data << 4));
} else if (id == 2001) { // Block break + block break sound
int blockId = data & 0xFFF;
int blockData = data >> 12;
wrapper.set(Type.INT, 1, data = WorldPackets.toNewId(blockId << 4 | blockData));
wrapper.set(Type.INT, 1, WorldPackets.toNewId(blockId << 4 | blockData));
}
}
});

View File

@ -35,7 +35,7 @@ public class RedstoneConnectionHandler extends ConnectionHandler {
b |= getState(data.getValue("north")) << 2;
b |= getState(data.getValue("south")) << 4;
b |= getState(data.getValue("west")) << 6;
b |= Integer.valueOf(data.getValue("power")) << 8;
b |= Integer.parseInt(data.getValue("power")) << 8;
return b;
}

View File

@ -21,13 +21,13 @@ import java.util.HashMap;
import java.util.Map;
public class MappingData {
public static BiMap<Integer, Integer> oldToNewItems = HashBiMap.create();
public static Map<String, Integer[]> blockTags = new HashMap<>();
public static Map<String, Integer[]> itemTags = new HashMap<>();
public static Map<String, Integer[]> fluidTags = new HashMap<>();
public static BiMap<Short, String> oldEnchantmentsIds = HashBiMap.create();
public static Map<String, String> translateMapping = new HashMap<>();
public static Map<String, String> mojangTranslation = new HashMap<>();
public static final BiMap<Integer, Integer> oldToNewItems = HashBiMap.create();
public static final Map<String, Integer[]> blockTags = new HashMap<>();
public static final Map<String, Integer[]> itemTags = new HashMap<>();
public static final Map<String, Integer[]> fluidTags = new HashMap<>();
public static final BiMap<Short, String> oldEnchantmentsIds = HashBiMap.create();
public static final Map<String, String> translateMapping = new HashMap<>();
public static final Map<String, String> mojangTranslation = new HashMap<>();
public static Mappings enchantmentMappings;
public static Mappings soundMappings;
public static Mappings blockMappings;
@ -50,7 +50,6 @@ public class MappingData {
Via.getPlatform().getLogger().info("Loading 1.12.2 -> 1.13 sound mapping...");
soundMappings = new SoundMappingShortArray(mapping1_12.getAsJsonArray("sounds"), mapping1_13.getAsJsonArray("sounds"));
Via.getPlatform().getLogger().info("Loading translation mappping");
translateMapping = new HashMap<>();
Map<String, String> translateData = GsonUtil.getGson().fromJson(
new InputStreamReader(
MappingData.class.getClassLoader()

View File

@ -10,8 +10,8 @@ import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.version.Types1_12;
import us.myles.ViaVersion.api.type.types.version.Types1_13;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.metadata.MetadataRewriter1_13To1_12_2;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.EntityTypeRewriter;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.metadata.MetadataRewriter1_13To1_12_2;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.EntityTracker1_13;
public class EntityPackets {
@ -68,11 +68,11 @@ public class EntityPackets {
}
wrapper.set(Type.INT, 0, data);
}
// Register Type ID
wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType);
}
}
// Register Type ID
wrapper.user().get(EntityTracker1_13.class).addEntity(entityId, entType);
}
});
}

View File

@ -624,14 +624,16 @@ public class InventoryPackets {
if (oldId == null && newId.startsWith("viaversion:legacy/")) {
oldId = Short.valueOf(newId.substring(18));
}
enchEntry.put(
new ShortTag(
"id",
oldId
)
);
enchEntry.put(new ShortTag("lvl", (Short) ((CompoundTag) enchantmentEntry).get("lvl").getValue()));
ench.add(enchEntry);
if (oldId != null) {
enchEntry.put(
new ShortTag(
"id",
oldId
)
);
enchEntry.put(new ShortTag("lvl", (Short) ((CompoundTag) enchantmentEntry).get("lvl").getValue()));
ench.add(enchEntry);
}
}
}
tag.remove("Enchantments");
@ -648,14 +650,16 @@ public class InventoryPackets {
if (oldId == null && newId.startsWith("viaversion:legacy/")) {
oldId = Short.valueOf(newId.substring(18));
}
enchEntry.put(
new ShortTag(
"id",
oldId
)
);
enchEntry.put(new ShortTag("lvl", (Short) ((CompoundTag) enchantmentEntry).get("lvl").getValue()));
newStoredEnch.add(enchEntry);
if (oldId != null) {
enchEntry.put(
new ShortTag(
"id",
oldId
)
);
enchEntry.put(new ShortTag("lvl", (Short) ((CompoundTag) enchantmentEntry).get("lvl").getValue()));
newStoredEnch.add(enchEntry);
}
}
}
tag.remove("StoredEnchantments");

View File

@ -9,11 +9,11 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.BlockEntityP
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.BlockStorage;
public class BannerHandler implements BlockEntityProvider.BlockEntityHandler {
private final int WALL_BANNER_START = 7110; // 4 each
private final int WALL_BANNER_STOP = 7173;
private static final int WALL_BANNER_START = 7110; // 4 each
private static final int WALL_BANNER_STOP = 7173;
private final int BANNER_START = 6854; // 16 each
private final int BANNER_STOP = 7109;
private static final int BANNER_START = 6854; // 16 each
private static final int BANNER_STOP = 7109;
@Override
public int transform(UserConnection user, CompoundTag tag) {

View File

@ -9,8 +9,8 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.BlockEntityP
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage.BlockStorage;
public class SkullHandler implements BlockEntityProvider.BlockEntityHandler {
private final int SKULL_WALL_START = 5447;
private final int SKULL_END = 5566;
private static final int SKULL_WALL_START = 5447;
private static final int SKULL_END = 5566;
@Override
public int transform(UserConnection user, CompoundTag tag) {

View File

@ -72,7 +72,7 @@ public class BlockStorage extends StoredObject {
@Data
@AllArgsConstructor
public class ReplacementData {
public static class ReplacementData {
private int original;
private int replacement;
}

View File

@ -12,7 +12,7 @@ import us.myles.ViaVersion.api.data.Mappings;
import java.util.*;
public class MappingData {
public static BiMap<Integer, Integer> oldToNewItems = HashBiMap.create();
public final static BiMap<Integer, Integer> oldToNewItems = HashBiMap.create();
public static Mappings blockStateMappings;
public static Mappings blockMappings;
public static Mappings soundMappings;

View File

@ -49,7 +49,6 @@ public class EntityPackets {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int entityId = wrapper.get(Type.VAR_INT, 0);
UUID uuid = wrapper.get(Type.UUID, 0);
int typeId = wrapper.get(Type.VAR_INT, 1);
Entity1_13Types.EntityType type1_13 = Entity1_13Types.getTypeFromId(typeId, true);
@ -95,11 +94,12 @@ public class EntityPackets {
velocity.write(Type.SHORT, wrapper.get(Type.SHORT, 2));
velocity.send(Protocol1_14To1_13_2.class);
}
// Register Type ID
wrapper.user().get(EntityTracker1_14.class).addEntity(entityId, type1_14);
}
wrapper.set(Type.VAR_INT, 1, typeId);
// Register Type ID
wrapper.user().get(EntityTracker1_14.class).addEntity(entityId, type1_14);
}
});
}
@ -128,14 +128,11 @@ public class EntityPackets {
public void handle(PacketWrapper wrapper) throws Exception {
int entityId = wrapper.get(Type.VAR_INT, 0);
int type = wrapper.get(Type.VAR_INT, 1);
UUID uuid = wrapper.get(Type.UUID, 0);
type = EntityTypeRewriter.getNewId(type).or(type);
Entity1_14Types.EntityType entType = Entity1_14Types.getTypeFromId(type);
wrapper.set(Type.VAR_INT, 1, type);
Entity1_14Types.EntityType entType = Entity1_14Types.getTypeFromId(type);
// Register Type ID
wrapper.user().get(EntityTracker1_14.class).addEntity(entityId, entType);
@ -173,7 +170,6 @@ public class EntityPackets {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
UUID uuid = wrapper.get(Type.UUID, 0);
int entityId = wrapper.get(Type.VAR_INT, 0);
Entity1_14Types.EntityType entType = Entity1_14Types.EntityType.PLAYER;

View File

@ -228,9 +228,9 @@ public class WorldPackets {
int id = wrapper.get(Type.INT, 0);
int data = wrapper.get(Type.INT, 1);
if (id == 1010) { // Play record
wrapper.set(Type.INT, 1, data = InventoryPackets.getNewItemId(data));
wrapper.set(Type.INT, 1, InventoryPackets.getNewItemId(data));
} else if (id == 2001) { // Block break + block break sound
wrapper.set(Type.INT, 1, data = Protocol1_14To1_13_2.getNewBlockStateId(data));
wrapper.set(Type.INT, 1, Protocol1_14To1_13_2.getNewBlockStateId(data));
}
}
});

View File

@ -34,7 +34,8 @@ public class Chunk1_9_1_2Type extends PartialType<Chunk, ClientWorld> {
boolean groundUp = input.readBoolean();
int primaryBitmask = Type.VAR_INT.read(input);
int size = Type.VAR_INT.read(input);
// Size (unused)
Type.VAR_INT.read(input);
BitSet usedSections = new BitSet(16);
ChunkSection[] sections = new ChunkSection[16];

View File

@ -329,7 +329,7 @@ public class WorldPackets {
@Override
public void registerMap() {
map(Type.POSITION); // 0 - Position
map(Type.VAR_INT, Type.BYTE); // 1 - Block Face
map(Type.VAR_INT, Type.UNSIGNED_BYTE); // 1 - Block Face
map(Type.VAR_INT, Type.NOTHING); // 2 - Hand
create(new ValueCreator() {
@Override
@ -359,7 +359,7 @@ public class WorldPackets {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int face = wrapper.get(Type.BYTE, 0);
int face = wrapper.get(Type.UNSIGNED_BYTE, 0);
if (face == 255)
return;
Position p = wrapper.get(Type.POSITION, 0);

View File

@ -100,14 +100,14 @@ public class UpdateUtil {
connection.setDoOutput(true);
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String input;
String content = "";
StringBuilder builder = new StringBuilder();
while ((input = br.readLine()) != null) {
content = content + input;
builder.append(input);
}
br.close();
JsonObject statistics;
try {
statistics = GsonUtil.getGson().fromJson(content, JsonObject.class);
statistics = GsonUtil.getGson().fromJson(builder.toString(), JsonObject.class);
} catch (JsonParseException e) {
e.printStackTrace();
return null;

View File

@ -76,10 +76,10 @@ public abstract class Config implements ConfigurationProvider {
defaults.remove(option);
}
// Merge with defaultLoader
for (Object key : config.keySet()) {
for (Map.Entry<String, Object> entry : config.entrySet()) {
// Set option in new conf if exists
if (defaults.containsKey(key) && !unsupported.contains(key.toString())) {
defaults.put((String) key, config.get(key));
if (defaults.containsKey(entry.getKey()) && !unsupported.contains(entry.getKey())) {
defaults.put(entry.getKey(), entry.getValue());
}
}
} catch (IOException e) {

View File

@ -70,18 +70,22 @@ public class HandItemCache implements Runnable {
}
}
int id = 0;
try {
id = (int) GET_ID.invoke(null, itemInHand.getItem());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
if (GET_ID != null) {
try {
id = (int) GET_ID.invoke(null, itemInHand.getItem());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
int damage = 0;
try {
damage = (int) GET_DAMAGE.get(itemInHand);
} catch (IllegalAccessException e) {
e.printStackTrace();
if (GET_DAMAGE != null) {
try {
damage = (int) GET_DAMAGE.get(itemInHand);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return new Item((short) id, (byte) itemInHand.getQuantity(), (short) damage, null);
}