use of com.github.steveice10.opennbt.tag.builtin.DoubleTag in project ViaVersion by ViaVersion.
the class InventoryPackets method registerPackets.
@Override
public void registerPackets() {
registerSetCooldown(ClientboundPackets1_13.COOLDOWN);
registerAdvancements(ClientboundPackets1_13.ADVANCEMENTS, Type.FLAT_VAR_INT_ITEM);
protocol.registerClientbound(ClientboundPackets1_13.OPEN_WINDOW, null, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
Short windowId = wrapper.read(Type.UNSIGNED_BYTE);
String type = wrapper.read(Type.STRING);
JsonElement title = wrapper.read(Type.COMPONENT);
COMPONENT_REWRITER.processText(title);
Short slots = wrapper.read(Type.UNSIGNED_BYTE);
if (type.equals("EntityHorse")) {
wrapper.setId(0x1F);
int entityId = wrapper.read(Type.INT);
wrapper.write(Type.UNSIGNED_BYTE, windowId);
wrapper.write(Type.VAR_INT, slots.intValue());
wrapper.write(Type.INT, entityId);
} else {
wrapper.setId(0x2E);
wrapper.write(Type.VAR_INT, windowId.intValue());
int typeId = -1;
switch(type) {
case "minecraft:crafting_table":
typeId = 11;
break;
case "minecraft:furnace":
typeId = 13;
break;
case "minecraft:dropper":
case "minecraft:dispenser":
typeId = 6;
break;
case "minecraft:enchanting_table":
typeId = 12;
break;
case "minecraft:brewing_stand":
typeId = 10;
break;
case "minecraft:villager":
typeId = 18;
break;
case "minecraft:beacon":
typeId = 8;
break;
case "minecraft:anvil":
typeId = 7;
break;
case "minecraft:hopper":
typeId = 15;
break;
case "minecraft:shulker_box":
typeId = 19;
break;
case "minecraft:container":
case "minecraft:chest":
default:
if (slots > 0 && slots <= 54) {
typeId = slots / 9 - 1;
}
break;
}
if (typeId == -1) {
Via.getPlatform().getLogger().warning("Can't open inventory for 1.14 player! Type: " + type + " Size: " + slots);
}
wrapper.write(Type.VAR_INT, typeId);
wrapper.write(Type.COMPONENT, title);
}
}
});
}
});
registerWindowItems(ClientboundPackets1_13.WINDOW_ITEMS, Type.FLAT_VAR_INT_ITEM_ARRAY);
registerSetSlot(ClientboundPackets1_13.SET_SLOT, Type.FLAT_VAR_INT_ITEM);
protocol.registerClientbound(ClientboundPackets1_13.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
// Channel
map(Type.STRING);
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
String channel = wrapper.get(Type.STRING, 0);
if (channel.equals("minecraft:trader_list") || channel.equals("trader_list")) {
wrapper.setId(0x27);
wrapper.resetReader();
// Remove channel
wrapper.read(Type.STRING);
int windowId = wrapper.read(Type.INT);
EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
tracker.setLatestTradeWindowId(windowId);
wrapper.write(Type.VAR_INT, windowId);
int size = wrapper.passthrough(Type.UNSIGNED_BYTE);
for (int i = 0; i < size; i++) {
// Input Item
handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
// Output Item
handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
// Has second item
boolean secondItem = wrapper.passthrough(Type.BOOLEAN);
if (secondItem) {
// Second Item
handleItemToClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
}
// Trade disabled
wrapper.passthrough(Type.BOOLEAN);
// Number of tools uses
wrapper.passthrough(Type.INT);
// Maximum number of trade uses
wrapper.passthrough(Type.INT);
wrapper.write(Type.INT, 0);
wrapper.write(Type.INT, 0);
wrapper.write(Type.FLOAT, 0f);
}
wrapper.write(Type.VAR_INT, 0);
wrapper.write(Type.VAR_INT, 0);
wrapper.write(Type.BOOLEAN, false);
} else if (channel.equals("minecraft:book_open") || channel.equals("book_open")) {
int hand = wrapper.read(Type.VAR_INT);
wrapper.clearPacket();
wrapper.setId(0x2D);
wrapper.write(Type.VAR_INT, hand);
}
}
});
}
});
registerEntityEquipment(ClientboundPackets1_13.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
RecipeRewriter recipeRewriter = new RecipeRewriter1_13_2(protocol);
protocol.registerClientbound(ClientboundPackets1_13.DECLARE_RECIPES, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
int size = wrapper.passthrough(Type.VAR_INT);
int deleted = 0;
for (int i = 0; i < size; i++) {
// Recipe Identifier
String id = wrapper.read(Type.STRING);
String type = wrapper.read(Type.STRING);
if (REMOVED_RECIPE_TYPES.contains(type)) {
deleted++;
continue;
}
wrapper.write(Type.STRING, type);
wrapper.write(Type.STRING, id);
recipeRewriter.handle(wrapper, type);
}
wrapper.set(Type.VAR_INT, 0, size - deleted);
});
}
});
registerClickWindow(ServerboundPackets1_14.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
protocol.registerServerbound(ServerboundPackets1_14.SELECT_TRADE, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
// Selecting trade now moves the items, we need to resync the inventory
PacketWrapper resyncPacket = wrapper.create(0x08);
EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
// 0 - Window ID
resyncPacket.write(Type.UNSIGNED_BYTE, ((short) tracker.getLatestTradeWindowId()));
// 1 - Slot
resyncPacket.write(Type.SHORT, ((short) -999));
// 2 - Button - End left click
resyncPacket.write(Type.BYTE, (byte) 2);
// 3 - Action number
resyncPacket.write(Type.SHORT, ((short) ThreadLocalRandom.current().nextInt()));
// 4 - Mode - Drag
resyncPacket.write(Type.VAR_INT, 5);
CompoundTag tag = new CompoundTag();
// Tags with NaN are not equal
tag.put("force_resync", new DoubleTag(Double.NaN));
// 5 - Clicked Item
resyncPacket.write(Type.FLAT_VAR_INT_ITEM, new DataItem(1, (byte) 1, (short) 0, tag));
resyncPacket.scheduleSendToServer(Protocol1_14To1_13_2.class);
}
});
}
});
registerCreativeInvAction(ServerboundPackets1_14.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.FLOAT);
}
use of com.github.steveice10.opennbt.tag.builtin.DoubleTag in project DragonProxy by DragonetMC.
the class ItemBlockTranslator method translateRawNBT.
@SuppressWarnings("unchecked")
public static org.dragonet.common.data.nbt.tag.CompoundTag translateRawNBT(int id, Tag pcTag, org.dragonet.common.data.nbt.tag.CompoundTag target) {
if (pcTag != null) {
String name = pcTag.getName() != null ? pcTag.getName() : "";
if (target == null)
target = new org.dragonet.common.data.nbt.tag.CompoundTag(name);
switch(pcTag.getClass().getSimpleName()) {
case "ByteArrayTag":
target.putByteArray(name, (byte[]) pcTag.getValue());
break;
case "ByteTag":
target.putByte(name, (byte) pcTag.getValue());
break;
case "DoubleTag":
target.putDouble(name, (double) pcTag.getValue());
break;
case "FloatTag":
target.putFloat(name, (float) pcTag.getValue());
break;
case "IntArrayTag":
target.putIntArray(name, (int[]) pcTag.getValue());
break;
case "IntTag":
target.putInt(name, (int) pcTag.getValue());
break;
case "LongTag":
target.putLong(name, (long) pcTag.getValue());
break;
case "ShortTag":
target.putShort(name, (short) pcTag.getValue());
break;
case "StringTag":
target.putString(name, (String) pcTag.getValue());
break;
case "CompoundTag":
for (String subName : ((CompoundTag) pcTag).getValue().keySet()) translateRawNBT(0, ((CompoundTag) pcTag).getValue().get(subName), target);
break;
case "ListTag":
ListTag listTag = new ListTag();
for (Tag subTag : (List<Tag>) pcTag.getValue()) listTag.add(translateRawNBT(0, subTag, new org.dragonet.common.data.nbt.tag.CompoundTag()));
target.putList(listTag);
break;
default:
System.out.println("TAG not implemented : " + pcTag.getClass().getSimpleName());
break;
}
}
return target;
}
use of com.github.steveice10.opennbt.tag.builtin.DoubleTag in project ViaVersion by ViaVersion.
the class TagStringReader method scalar.
/**
* A tag that is definitely some sort of scalar.
*
* <p>Does not detect quoted strings, so those should have been parsed already.</p>
*
* @return a parsed tag
*/
private Tag scalar() {
final StringBuilder builder = new StringBuilder();
int noLongerNumericAt = -1;
while (this.buffer.hasMore()) {
char current = this.buffer.peek();
if (current == '\\') {
// escape -- we are significantly more lenient than original format at the moment
this.buffer.advance();
current = this.buffer.take();
} else if (Tokens.id(current)) {
this.buffer.advance();
} else {
// end of value
break;
}
builder.append(current);
if (noLongerNumericAt == -1 && !Tokens.numeric(current)) {
noLongerNumericAt = builder.length();
}
}
final int length = builder.length();
final String built = builder.toString();
if (noLongerNumericAt == length) {
final char last = built.charAt(length - 1);
try {
switch(// try to read and return as a number
Character.toLowerCase(last)) {
case Tokens.TYPE_BYTE:
return new ByteTag(Byte.parseByte(built.substring(0, length - 1)));
case Tokens.TYPE_SHORT:
return new ShortTag(Short.parseShort(built.substring(0, length - 1)));
case Tokens.TYPE_INT:
return new IntTag(Integer.parseInt(built.substring(0, length - 1)));
case Tokens.TYPE_LONG:
return new LongTag(Long.parseLong(built.substring(0, length - 1)));
case Tokens.TYPE_FLOAT:
final float floatValue = Float.parseFloat(built.substring(0, length - 1));
if (Float.isFinite(floatValue)) {
// don't accept NaN and Infinity
return new FloatTag(floatValue);
}
break;
case Tokens.TYPE_DOUBLE:
final double doubleValue = Double.parseDouble(built.substring(0, length - 1));
if (Double.isFinite(doubleValue)) {
// don't accept NaN and Infinity
return new DoubleTag(doubleValue);
}
break;
}
} catch (final NumberFormatException ignored) {
}
} else if (noLongerNumericAt == -1) {
// if we run out of content without an explicit value separator, then we're either an integer or string tag -- all others have a character at the end
try {
return new IntTag(Integer.parseInt(built));
} catch (final NumberFormatException ex) {
if (built.indexOf('.') != -1) {
try {
return new DoubleTag(Double.parseDouble(built));
} catch (final NumberFormatException ex2) {
// ignore
}
}
}
}
if (built.equalsIgnoreCase(Tokens.LITERAL_TRUE)) {
return new ByteTag((byte) 1);
} else if (built.equalsIgnoreCase(Tokens.LITERAL_FALSE)) {
return new ByteTag((byte) 0);
}
return new StringTag(built);
}
Aggregations