3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-28 00:50:13 +01:00

Use Map in RecipeRewriter, use correct names for 1.13/1.14 rewriters

Dieser Commit ist enthalten in:
KennyTV 2020-07-15 11:42:59 +02:00
Ursprung 42167394e0
Commit e1464044a7
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B
8 geänderte Dateien mit 115 neuen und 200 gelöschten Zeilen

Datei anzeigen

@ -6,17 +6,26 @@ import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.type.Type;
import java.util.HashMap;
import java.util.Map;
public abstract class RecipeRewriter {
protected final Protocol protocol;
protected final ItemRewriter.RewriteFunction rewriter;
protected final Map<String, RecipeConsumer> recipeHandlers = new HashMap<>();
protected RecipeRewriter(Protocol protocol, ItemRewriter.RewriteFunction rewriter) {
this.protocol = protocol;
this.rewriter = rewriter;
}
public abstract void handle(PacketWrapper wrapper, String type) throws Exception;
public void handle(PacketWrapper wrapper, String type) throws Exception {
RecipeConsumer handler = recipeHandlers.get(type);
if (handler != null) {
handler.accept(wrapper);
}
}
public void registerDefaultHandler(ClientboundPacketType packetType) {
protocol.registerOutgoing(packetType, new PacketRemapper() {
@ -33,4 +42,10 @@ public abstract class RecipeRewriter {
}
});
}
@FunctionalInterface
public interface RecipeConsumer {
void accept(PacketWrapper wrapper) throws Exception;
}
}

Datei anzeigen

@ -6,9 +6,11 @@ import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.rewriters.ItemRewriter;
import us.myles.ViaVersion.api.rewriters.RecipeRewriter;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ServerboundPackets1_13;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2;
public class InventoryPackets {
@ -56,49 +58,17 @@ public class InventoryPackets {
itemRewriter.registerEntityEquipment(ClientboundPackets1_13.ENTITY_EQUIPMENT, Type.FLAT_ITEM);
RecipeRewriter recipeRewriter = new RecipeRewriter1_13_2(protocol, InventoryPackets::toClient);
protocol.registerOutgoing(ClientboundPackets1_13.DECLARE_RECIPES, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int recipesNo = wrapper.passthrough(Type.VAR_INT);
for (int i = 0; i < recipesNo; i++) {
wrapper.passthrough(Type.STRING); // Id
String type = wrapper.passthrough(Type.STRING);
if (type.equals("crafting_shapeless")) {
wrapper.passthrough(Type.STRING); // Group
int ingredientsNo = wrapper.passthrough(Type.VAR_INT);
for (int i1 = 0; i1 < ingredientsNo; i1++) {
Item[] items = wrapper.passthrough(Type.FLAT_ITEM_ARRAY_VAR_INT);
for (Item item : items) {
InventoryPackets.toClient(item);
}
}
InventoryPackets.toClient(wrapper.passthrough(Type.FLAT_ITEM)); // Result
} else if (type.equals("crafting_shaped")) {
int ingredientsNo = wrapper.passthrough(Type.VAR_INT) * wrapper.passthrough(Type.VAR_INT);
wrapper.passthrough(Type.STRING); // Group
for (int i1 = 0; i1 < ingredientsNo; i1++) {
Item[] items = wrapper.passthrough(Type.FLAT_ITEM_ARRAY_VAR_INT);
for (Item item : items) {
InventoryPackets.toClient(item);
}
}
InventoryPackets.toClient(wrapper.passthrough(Type.FLAT_ITEM)); // Result
} else if (type.equals("smelting")) {
wrapper.passthrough(Type.STRING); // Group
// Ingredient start
Item[] items = wrapper.passthrough(Type.FLAT_ITEM_ARRAY_VAR_INT);
for (Item item : items) {
InventoryPackets.toClient(item);
}
// Ingredient end
InventoryPackets.toClient(wrapper.passthrough(Type.FLAT_ITEM));
wrapper.passthrough(Type.FLOAT); // EXP
wrapper.passthrough(Type.VAR_INT); // Cooking time
}
}
handler(wrapper -> {
int size = wrapper.passthrough(Type.VAR_INT);
for (int i = 0; i < size; i++) {
// First type, then id
String type = wrapper.passthrough(Type.STRING).replace("minecraft:", "");
String id = wrapper.passthrough(Type.STRING); // Recipe Identifier
recipeRewriter.handle(wrapper, type);
}
});
}

Datei anzeigen

@ -0,0 +1,58 @@
package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.rewriters.ItemRewriter;
import us.myles.ViaVersion.api.rewriters.RecipeRewriter;
import us.myles.ViaVersion.api.type.Type;
/**
* For 1.13.2, not 1.13 (1.13 reads recipe type and id in swapped order)!
*/
public class RecipeRewriter1_13_2 extends RecipeRewriter {
public RecipeRewriter1_13_2(Protocol protocol, ItemRewriter.RewriteFunction rewriter) {
super(protocol, rewriter);
recipeHandlers.put("crafting_shapeless", this::handleCraftingShapeless);
recipeHandlers.put("crafting_shaped", this::handleCraftingShaped);
recipeHandlers.put("smelting", this::handleSmelting);
}
public void handleSmelting(PacketWrapper wrapper) throws Exception {
wrapper.passthrough(Type.STRING); // Group
Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
for (Item item : items) {
rewriter.rewrite(item);
}
rewriter.rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
wrapper.passthrough(Type.FLOAT); // EXP
wrapper.passthrough(Type.VAR_INT); // Cooking time
}
public void handleCraftingShaped(PacketWrapper wrapper) throws Exception {
int ingredientsNo = wrapper.passthrough(Type.VAR_INT) * wrapper.passthrough(Type.VAR_INT);
wrapper.passthrough(Type.STRING); // Group
for (int j = 0; j < ingredientsNo; j++) {
Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
for (Item item : items) {
rewriter.rewrite(item);
}
}
rewriter.rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
}
public void handleCraftingShapeless(PacketWrapper wrapper) throws Exception {
wrapper.passthrough(Type.STRING); // Group
int ingredientsNo = wrapper.passthrough(Type.VAR_INT);
for (int j = 0; j < ingredientsNo; j++) {
Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
for (Item item : items) {
rewriter.rewrite(item);
}
}
rewriter.rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
}
}

Datei anzeigen

@ -4,63 +4,27 @@ import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.rewriters.ItemRewriter;
import us.myles.ViaVersion.api.rewriters.RecipeRewriter;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2;
public class RecipeRewriter1_14 extends RecipeRewriter {
public class RecipeRewriter1_14 extends RecipeRewriter1_13_2 {
public RecipeRewriter1_14(Protocol protocol, ItemRewriter.RewriteFunction rewriter) {
super(protocol, rewriter);
recipeHandlers.put("stonecutting", this::handleStonecutting);
recipeHandlers.put("blasting", this::handleSmelting);
recipeHandlers.put("smoking", this::handleSmelting);
recipeHandlers.put("campfire_cooking", this::handleSmelting);
}
public void handle(PacketWrapper wrapper, String type) throws Exception {
switch (type) {
case "crafting_shapeless":
handleCraftingShapeless(wrapper);
break;
case "crafting_shaped":
handleCraftingShaped(wrapper);
break;
case "smelting":
handleSmelting(wrapper);
break;
}
}
public void handleSmelting(PacketWrapper wrapper) throws Exception {
wrapper.passthrough(Type.STRING); // Group
public void handleStonecutting(PacketWrapper wrapper) throws Exception {
wrapper.passthrough(Type.STRING);
Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
for (Item item : items) {
rewriter.rewrite(item);
}
rewriter.rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
wrapper.passthrough(Type.FLOAT); // EXP
wrapper.passthrough(Type.VAR_INT); // Cooking time
}
public void handleCraftingShaped(PacketWrapper wrapper) throws Exception {
int ingredientsNo = wrapper.passthrough(Type.VAR_INT) * wrapper.passthrough(Type.VAR_INT);
wrapper.passthrough(Type.STRING); // Group
for (int j = 0; j < ingredientsNo; j++) {
Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
for (Item item : items) {
rewriter.rewrite(item);
}
}
rewriter.rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
}
public void handleCraftingShapeless(PacketWrapper wrapper) throws Exception {
wrapper.passthrough(Type.STRING); // Group
int ingredientsNo = wrapper.passthrough(Type.VAR_INT);
for (int j = 0; j < ingredientsNo; j++) {
Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
for (Item item : items) {
rewriter.rewrite(item);
}
}
rewriter.rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
}
}

Datei anzeigen

@ -17,9 +17,11 @@ import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.rewriters.ComponentRewriter;
import us.myles.ViaVersion.api.rewriters.ItemRewriter;
import us.myles.ViaVersion.api.rewriters.RecipeRewriter;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData;
@ -178,51 +180,26 @@ public class InventoryPackets {
itemRewriter.registerEntityEquipment(ClientboundPackets1_13.ENTITY_EQUIPMENT, Type.FLAT_VAR_INT_ITEM);
RecipeRewriter recipeRewriter = new RecipeRewriter1_13_2(protocol, InventoryPackets::toClient);
protocol.registerOutgoing(ClientboundPackets1_13.DECLARE_RECIPES, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int size = wrapper.passthrough(Type.VAR_INT);
int deleted = 0;
for (int i = 0; i < size; i++) {
String id = wrapper.read(Type.STRING); // Recipe Identifier
String type = wrapper.read(Type.STRING);
if (REMOVED_RECIPE_TYPES.contains(type)) {
deleted++;
continue;
}
wrapper.write(Type.STRING, type);
wrapper.write(Type.STRING, id);
if (type.equals("crafting_shapeless")) {
wrapper.passthrough(Type.STRING); // Group
int ingredientsNo = wrapper.passthrough(Type.VAR_INT);
for (int j = 0; j < ingredientsNo; j++) {
Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
for (Item item : items) toClient(item);
}
toClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
} else if (type.equals("crafting_shaped")) {
int ingredientsNo = wrapper.passthrough(Type.VAR_INT) * wrapper.passthrough(Type.VAR_INT);
wrapper.passthrough(Type.STRING); // Group
for (int j = 0; j < ingredientsNo; j++) {
Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
for (Item item : items) toClient(item);
}
toClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
} else if (type.equals("smelting")) {
wrapper.passthrough(Type.STRING); // Group
Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
for (Item item : items) toClient(item);
toClient(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM));
wrapper.passthrough(Type.FLOAT); // EXP
wrapper.passthrough(Type.VAR_INT); // Cooking time
}
handler(wrapper -> {
int size = wrapper.passthrough(Type.VAR_INT);
int deleted = 0;
for (int i = 0; i < size; i++) {
String id = wrapper.read(Type.STRING); // Recipe Identifier
String type = wrapper.read(Type.STRING);
if (REMOVED_RECIPE_TYPES.contains(type)) {
deleted++;
continue;
}
wrapper.set(Type.VAR_INT, 0, size - deleted);
wrapper.write(Type.STRING, type);
wrapper.write(Type.STRING, id);
recipeRewriter.handle(wrapper, type);
}
wrapper.set(Type.VAR_INT, 0, size - deleted);
});
}
});

Datei anzeigen

@ -1,46 +0,0 @@
package us.myles.ViaVersion.protocols.protocol1_15to1_14_4.data;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.rewriters.ItemRewriter;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14;
public class RecipeRewriter1_15 extends RecipeRewriter1_14 {
public RecipeRewriter1_15(Protocol protocol, ItemRewriter.RewriteFunction rewriter) {
super(protocol, rewriter);
}
@Override
public void handle(PacketWrapper wrapper, String type) throws Exception {
switch (type) {
case "crafting_shapeless":
handleCraftingShapeless(wrapper);
break;
case "crafting_shaped":
handleCraftingShaped(wrapper);
break;
case "blasting": // new
case "smoking": // new
case "campfire_cooking": // new
case "smelting":
handleSmelting(wrapper);
break;
case "stonecutting": // new
handleStonecutting(wrapper);
break;
}
}
public void handleStonecutting(PacketWrapper wrapper) throws Exception {
wrapper.passthrough(Type.STRING);
Item[] items = wrapper.passthrough(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT); // Ingredients
for (Item item : items) {
rewriter.rewrite(item);
}
rewriter.rewrite(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)); // Result
}
}

Datei anzeigen

@ -5,36 +5,13 @@ import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.rewriters.ItemRewriter;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.data.RecipeRewriter1_15;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14;
public class RecipeRewriter1_16 extends RecipeRewriter1_15 {
public class RecipeRewriter1_16 extends RecipeRewriter1_14 {
public RecipeRewriter1_16(Protocol protocol, ItemRewriter.RewriteFunction rewriter) {
super(protocol, rewriter);
}
@Override
public void handle(PacketWrapper wrapper, String type) throws Exception {
switch (type) {
case "crafting_shapeless":
handleCraftingShapeless(wrapper);
break;
case "crafting_shaped":
handleCraftingShaped(wrapper);
break;
case "blasting":
case "smoking":
case "campfire_cooking":
case "smelting":
handleSmelting(wrapper);
break;
case "stonecutting":
handleStonecutting(wrapper);
break;
case "smithing": // new
handleSmithing(wrapper);
break;
}
recipeHandlers.put("smithing", this::handleSmithing);
}
public void handleSmithing(PacketWrapper wrapper) throws Exception {

Datei anzeigen

@ -14,7 +14,7 @@ import us.myles.ViaVersion.api.rewriters.ItemRewriter;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.UUIDIntArrayType;
import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15;
import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.data.RecipeRewriter1_15;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.RecipeRewriter1_14;
import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.ServerboundPackets1_16;
import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.data.MappingData;
@ -78,7 +78,7 @@ public class InventoryPackets {
}
});
new RecipeRewriter1_15(protocol, InventoryPackets::toClient).registerDefaultHandler(ClientboundPackets1_15.DECLARE_RECIPES);
new RecipeRewriter1_14(protocol, InventoryPackets::toClient).registerDefaultHandler(ClientboundPackets1_15.DECLARE_RECIPES);
itemRewriter.registerClickWindow(ServerboundPackets1_16.CLICK_WINDOW, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerCreativeInvAction(ServerboundPackets1_16.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);