3
0
Mirror von https://github.com/ViaVersion/ViaBackwards.git synchronisiert 2024-07-26 17:08:05 +02:00

Minor optimizations to legacy block rewriting

Dieser Commit ist enthalten in:
KennyTV 2020-01-26 15:41:25 +01:00
Ursprung 96b9a023c1
Commit b62c8c46e3
6 geänderte Dateien mit 90 neuen und 77 gelöschten Zeilen

Datei anzeigen

@ -29,12 +29,11 @@ import us.myles.viaversion.libs.opennbt.tag.builtin.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public abstract class BlockItemRewriter<T extends BackwardsProtocol> extends Rewriter<T> {
private static final CompoundTagConverter converter = new CompoundTagConverter();
private final Map<Integer, BlockItemSettings> replacementData = new ConcurrentHashMap<>();
private final Map<Integer, BlockItemSettings> replacementData = new HashMap<>();
protected String nbtTagName;
protected boolean jsonNameFormat = true;
@ -60,6 +59,18 @@ public abstract class BlockItemRewriter<T extends BackwardsProtocol> extends Rew
ItemUtil.copyItem(i, data.getRepItem());
if (i.getTag() == null) {
i.setTag(new CompoundTag(""));
} else {
// Handle colors
CompoundTag tag = i.getTag().get("display");
if (tag != null) {
StringTag nameTag = tag.get("Name");
if (nameTag != null) {
String value = nameTag.getValue();
if (value.contains("%vb_color%")) {
tag.put(new StringTag("Name", value.replace("%vb_color%", BlockColors.get(original.getData()))));
}
}
}
}
// Backup data for toServer
@ -72,16 +83,6 @@ public abstract class BlockItemRewriter<T extends BackwardsProtocol> extends Rew
}
}
// Handle colors
if (i.getTag().contains("display")) {
CompoundTag tag = i.getTag().get("display");
if (tag.contains("Name")) {
String value = (String) tag.get("Name").getValue();
tag.put(new StringTag("Name",
value.replaceAll("%viabackwards_color%", BlockColors.get((int) original.getData()))));
}
}
i.setAmount(original.getAmount());
// Keep original data when -1
if (i.getData() == -1) {
@ -125,22 +126,22 @@ public abstract class BlockItemRewriter<T extends BackwardsProtocol> extends Rew
int type = idx >> 4;
int meta = idx & 15;
if (!containsBlock(type))
return idx;
Block b = handleBlock(type, meta);
if (b == null) return idx;
return (b.getId() << 4 | (b.getData() & 15));
}
public Block handleBlock(int block, int data) {
if (!containsBlock(block))
return null;
public Block handleBlock(int blockId, int data) {
BlockItemSettings settings = replacementData.get(blockId);
if (settings == null || !settings.hasRepBlock()) return null;
Block b = replacementData.get(block).getRepBlock().clone();
Block block = settings.getRepBlock();
// For some blocks, the data can still be useful (:
if (b.getData() == -1)
b.setData(data);
return b;
if (block.getData() == -1) {
return block.withData(data);
}
return block;
}
protected void handleChunk(Chunk chunk) {
@ -160,14 +161,16 @@ public abstract class BlockItemRewriter<T extends BackwardsProtocol> extends Rew
if (section == null) continue;
int block = section.getFlatBlock(pos.getX(), pos.getY() & 0xF, pos.getZ());
int btype = block >> 4;
if (!hasBlockEntityHandler(btype)) continue;
replacementData.get(btype).getBlockEntityHandler().handleOrNewCompoundTag(block, tag);
BlockItemSettings settings = replacementData.get(btype);
if (settings != null && settings.hasEntityHandler()) {
settings.getBlockEntityHandler().handleOrNewCompoundTag(block, tag);
}
}
for (int i = 0; i < chunk.getSections().length; i++) {
ChunkSection section = chunk.getSections()[i];
if (section == null)
continue;
if (section == null) continue;
boolean hasBlockEntityHandler = false;
@ -177,12 +180,18 @@ public abstract class BlockItemRewriter<T extends BackwardsProtocol> extends Rew
int btype = block >> 4;
int meta = block & 0xF;
if (containsBlock(btype)) {
Block b = handleBlock(btype, meta);
Block b = handleBlock(btype, meta);
if (b != null) {
section.setPaletteEntry(j, (b.getId() << 4) | (b.getData() & 0xF));
}
hasBlockEntityHandler = hasBlockEntityHandler || hasBlockEntityHandler(btype);
// We already know that is has a handler
if (hasBlockEntityHandler) continue;
BlockItemSettings settings = replacementData.get(btype);
if (section != null && settings.hasEntityHandler()) {
hasBlockEntityHandler = true;
}
}
if (!hasBlockEntityHandler) continue;
@ -195,7 +204,8 @@ public abstract class BlockItemRewriter<T extends BackwardsProtocol> extends Rew
int btype = block >> 4;
int meta = block & 15;
if (!hasBlockEntityHandler(btype)) continue;
BlockItemSettings settings = replacementData.get(btype);
if (settings == null || !settings.hasEntityHandler()) continue;
Pos pos = new Pos(x, (y + (i << 4)), z);
@ -206,7 +216,8 @@ public abstract class BlockItemRewriter<T extends BackwardsProtocol> extends Rew
tag.put(new IntTag("x", x + (chunk.getX() << 4)));
tag.put(new IntTag("y", y + (i << 4)));
tag.put(new IntTag("z", z + (chunk.getZ() << 4)));
replacementData.get(btype).getBlockEntityHandler().handleOrNewCompoundTag(block, tag);
settings.getBlockEntityHandler().handleOrNewCompoundTag(block, tag);
chunk.getBlockEntities().add(tag);
}
}

Datei anzeigen

@ -356,7 +356,7 @@ public class BlockItemPackets1_11 extends BlockItemRewriter<Protocol1_10To1_11>
// ShulkerBoxes to Dropper
for (int i = 219; i < 235; i++)
rewrite(i).repItem(new Item(158, (byte) 1, (short) 0, getNamedTag("1.11 " + BlockColors.get(i - 219) + " Shulker Box")))
.repBlock(new Block(158, 0));
.repBlock(new Block(158));
// Observer to Dispenser
rewrite(218).repItem(new Item(23, (byte) 1, (short) 0, getNamedTag("1.11 Observer"))).repBlock(new Block(23, -1));

Datei anzeigen

@ -10,41 +10,29 @@
package nl.matsv.viabackwards.protocol.protocol1_11_1to1_12.data;
import java.util.HashMap;
import java.util.Map;
public class BlockColors {
private static final Map<Integer, String> COLORS = new HashMap<>();
private static int count = 0;
private static final String[] COLORS = new String[16];
static {
add("White");
add("Orange");
add("Magenta");
add("Light Blue");
add("Yellow");
add("Lime");
add("Pink");
add("Gray");
add("Light Gray");
add("Cyan");
add("Purple");
add("Blue");
add("Brown");
add("Green");
add("Red");
add("Black");
COLORS[0] = "White";
COLORS[1] = "Orange";
COLORS[2] = "Magenta";
COLORS[3] = "Light Blue";
COLORS[4] = "Yellow";
COLORS[5] = "Lime";
COLORS[6] = "Pink";
COLORS[7] = "Gray";
COLORS[8] = "Light Gray";
COLORS[9] = "Cyan";
COLORS[10] = "Purple";
COLORS[11] = "Blue";
COLORS[12] = "Brown";
COLORS[13] = "Green";
COLORS[14] = "Red";
COLORS[15] = "Black";
}
private static void add(String value) {
COLORS.put(count++, value);
}
public static boolean has(Integer key) {
return COLORS.containsKey(key);
}
public static String get(Integer key) {
return COLORS.getOrDefault(key, "Unknown color");
public static String get(int key) {
return key >= 0 && key < COLORS.length ? COLORS[key] : "Unknown color";
}
}

Datei anzeigen

@ -269,12 +269,12 @@ public class BlockItemPackets1_12 extends BlockItemRewriter<Protocol1_11_1To1_12
protected void registerRewrites() {
// Concrete -> Stained clay? (Also got a new name Terracota?)
rewrite(251)
.repItem(new Item(159, (byte) 1, (short) -1, getNamedTag("1.12 %viabackwards_color% Concrete")))
.repItem(new Item(159, (byte) 1, (short) -1, getNamedTag("1.12 %vb_color% Concrete")))
.repBlock(new Block(159, -1));
// Concrete Powder -> Wool
rewrite(252)
.repItem(new Item(35, (byte) 1, (short) -1, getNamedTag("1.12 %viabackwards_color% Concrete Powder")))
.repItem(new Item(35, (byte) 1, (short) -1, getNamedTag("1.12 %vb_color% Concrete Powder")))
.repBlock(new Block(35, -1));
// Knowledge book -> book
@ -296,6 +296,6 @@ public class BlockItemPackets1_12 extends BlockItemRewriter<Protocol1_11_1To1_12
}
// Handle beds
rewrite(355).repItem(new Item(355, (byte) 1, (short) 0, getNamedTag("1.12 %viabackwards_color% Bed")));
rewrite(355).repItem(new Item(355, (byte) 1, (short) 0, getNamedTag("1.12 %vb_color% Bed")));
}
}

Datei anzeigen

@ -183,10 +183,10 @@ public class BlockItemPackets1_10 extends BlockItemRewriter<Protocol1_9_4To1_10>
@Override
protected void registerRewrites() {
rewrite(255).repItem(new Item(166, (byte) 1, (short) 0, getNamedTag("1.10 Structure Block"))); // Structure block only item since the structure block is in 1.9
rewrite(217).repItem(new Item(287, (byte) 1, (short) 0, getNamedTag("1.10 Structure Void"))).repBlock(new Block(287, 0)); // Structure void to string
rewrite(217).repItem(new Item(287, (byte) 1, (short) 0, getNamedTag("1.10 Structure Void"))).repBlock(new Block(287)); // Structure void to string
rewrite(213).repItem(new Item(159, (byte) 1, (short) 1, getNamedTag("1.10 Magma Block"))).repBlock(new Block(159, 1)); // Magma block to orange clay
rewrite(214).repItem(new Item(159, (byte) 1, (short) 14, getNamedTag("1.10 Nether Wart Block"))).repBlock(new Block(159, 14)); // Nether wart block to red clay
rewrite(215).repItem(new Item(112, (byte) 1, (short) 0, getNamedTag("1.10 Red Nether Bricks"))).repBlock(new Block(112, 0)); // Red nether brick to nether brick
rewrite(216).repItem(new Item(155, (byte) 1, (short) 0, getNamedTag("1.10 Bone Block"))).repBlock(new Block(155, 0)); // Bone block to quartz
rewrite(215).repItem(new Item(112, (byte) 1, (short) 0, getNamedTag("1.10 Red Nether Bricks"))).repBlock(new Block(112)); // Red nether brick to nether brick
rewrite(216).repItem(new Item(155, (byte) 1, (short) 0, getNamedTag("1.10 Bone Block"))).repBlock(new Block(155)); // Bone block to quartz
}
}

Datei anzeigen

@ -10,18 +10,32 @@
package nl.matsv.viabackwards.utils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
@AllArgsConstructor
@EqualsAndHashCode
@Data
public class Block {
private int id;
private int data;
private final int id;
private final int data;
public Block clone() {
return new Block(id, data);
public Block(final int id, final int data) {
this.id = id;
this.data = data;
}
public Block(final int id) {
this.id = id;
this.data = 0;
}
public int getId() {
return id;
}
public int getData() {
return data;
}
public Block withData(final int data) {
return new Block(this.id, data);
}
}