Pot handler rewrite

This commit is contained in:
creeper123123321 2019-11-04 17:36:16 -03:00
parent f8b4a6e28a
commit 3761ebf8c7

View File

@ -2,7 +2,6 @@ package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.blockentiti
import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.Pair;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.BlockEntityProvider; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.BlockEntityProvider;
@ -10,62 +9,66 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
public class FlowerPotHandler implements BlockEntityProvider.BlockEntityHandler { public class FlowerPotHandler implements BlockEntityProvider.BlockEntityHandler {
private static final Map<Pair<String, Byte>, Integer> flowers = new ConcurrentHashMap<>(); // Object -> string (id without namespace) or byte (numeric id)
private static final Map<Pair<Byte, Byte>, Integer> flowersNumberId = new ConcurrentHashMap<>(); private static final Map<Pair<?, Byte>, Integer> flowers = new ConcurrentHashMap<>();
static { static {
register("minecraft:air", (byte) 0, (byte) 0, 5265); register("air", (byte) 0, (byte) 0, 5265);
register("minecraft:sapling", (byte) 6, (byte) 0, 5266); register("sapling", (byte) 6, (byte) 0, 5266);
register("minecraft:sapling", (byte) 6, (byte) 1, 5267); register("sapling", (byte) 6, (byte) 1, 5267);
register("minecraft:sapling", (byte) 6, (byte) 2, 5268); register("sapling", (byte) 6, (byte) 2, 5268);
register("minecraft:sapling", (byte) 6, (byte) 3, 5269); register("sapling", (byte) 6, (byte) 3, 5269);
register("minecraft:sapling", (byte) 6, (byte) 4, 5270); register("sapling", (byte) 6, (byte) 4, 5270);
register("minecraft:sapling", (byte) 6, (byte) 5, 5271); register("sapling", (byte) 6, (byte) 5, 5271);
register("minecraft:tallgrass", (byte) 31, (byte) 2, 5272); register("tallgrass", (byte) 31, (byte) 2, 5272);
register("minecraft:yellow_flower", (byte) 37, (byte) 0, 5273); register("yellow_flower", (byte) 37, (byte) 0, 5273);
register("minecraft:red_flower", (byte) 38, (byte) 0, 5274); register("red_flower", (byte) 38, (byte) 0, 5274);
register("minecraft:red_flower", (byte) 38, (byte) 1, 5275); register("red_flower", (byte) 38, (byte) 1, 5275);
register("minecraft:red_flower", (byte) 38, (byte) 2, 5276); register("red_flower", (byte) 38, (byte) 2, 5276);
register("minecraft:red_flower", (byte) 38, (byte) 3, 5277); register("red_flower", (byte) 38, (byte) 3, 5277);
register("minecraft:red_flower", (byte) 38, (byte) 4, 5278); register("red_flower", (byte) 38, (byte) 4, 5278);
register("minecraft:red_flower", (byte) 38, (byte) 5, 5279); register("red_flower", (byte) 38, (byte) 5, 5279);
register("minecraft:red_flower", (byte) 38, (byte) 6, 5280); register("red_flower", (byte) 38, (byte) 6, 5280);
register("minecraft:red_flower", (byte) 38, (byte) 7, 5281); register("red_flower", (byte) 38, (byte) 7, 5281);
register("minecraft:red_flower", (byte) 38, (byte) 8, 5282); register("red_flower", (byte) 38, (byte) 8, 5282);
register("minecraft:red_mushroom", (byte) 40, (byte) 0, 5283); register("red_mushroom", (byte) 40, (byte) 0, 5283);
register("minecraft:brown_mushroom", (byte) 39, (byte) 0, 5284); register("brown_mushroom", (byte) 39, (byte) 0, 5284);
register("minecraft:deadbush", (byte) 32, (byte) 0, 5285); register("deadbush", (byte) 32, (byte) 0, 5285);
register("minecraft:cactus", (byte) 81, (byte) 0, 5286); register("cactus", (byte) 81, (byte) 0, 5286);
} }
public static void register(String identifier, byte numbericBlockId, byte blockData, int newId) { public static void register(String identifier, byte numbericBlockId, byte blockData, int newId) {
flowers.put(new Pair<>(identifier, blockData), newId); flowers.put(new Pair<>(identifier, blockData), newId);
flowersNumberId.put(new Pair<>(numbericBlockId, blockData), newId); flowers.put(new Pair<>(numbericBlockId, blockData), newId);
} }
@Override @Override
public int transform(UserConnection user, CompoundTag tag) { public int transform(UserConnection user, CompoundTag tag) {
Object item = tag.get("Item").getValue(); Object item = tag.contains("Item") ? tag.get("Item").getValue() : null;
byte data = ((Number) tag.get("Data").getValue()).byteValue(); Object data = tag.contains("Data") ? tag.get("Data").getValue() : null;
Pair<?, Byte> pair = item instanceof Number // Convert item to String without namespace or to Byte
? new Pair<>(((Number) item).byteValue(), data) if (item instanceof String) {
: new Pair<>((String) item, data); item = ((String) item).replace("minecraft:", "");
} else if (item instanceof Number) {
// Return air on empty string item = ((Number) item).byteValue();
if (item instanceof String && ((String) item).isEmpty())
return 5265;
else if (flowers.containsKey(pair)) {
return flowers.get(pair);
} else if (flowersNumberId.containsKey(pair)) {
return flowersNumberId.get(pair);
} else { } else {
if (!Via.getConfig().isSuppress1_13ConversionErrors() || Via.getManager().isDebug()) { item = (byte) 0;
Via.getPlatform().getLogger().warning("Could not find flowerpot content " + item + " for " + tag);
}
} }
return -1; // Convert data to Byte
if (data instanceof Number) {
data = ((Number) data).byteValue();
} else {
data = (byte) 0;
}
Integer flower = flowers.get(new Pair<>(item, (byte) data));
if (flower != null) return flower;
flower = flowers.get(new Pair<>(item, (byte) 0));
if (flower != null) return flower;
return 5265; // Fallback to empty pot
} }
} }