mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-26 18:39:20 +00:00
0.0.64.8
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package net.momirealms.craftengine.core.advancement.network;
|
||||
|
||||
import net.momirealms.craftengine.core.entity.player.Player;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.momirealms.craftengine.core.item.Item;
|
||||
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.VersionHelper;
|
||||
@@ -8,10 +9,11 @@ import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class Advancement {
|
||||
public class Advancement<I> {
|
||||
private final Optional<Key> parent;
|
||||
private final Optional<AdvancementDisplay> displayInfo;
|
||||
private final Optional<AdvancementDisplay<I>> displayInfo;
|
||||
|
||||
// 1.20-1.20.1
|
||||
private final Map<String, Void> criteria;
|
||||
@@ -19,7 +21,7 @@ public class Advancement {
|
||||
private final AdvancementRequirements requirements;
|
||||
private final boolean sendsTelemetryEvent;
|
||||
|
||||
public Advancement(Optional<Key> parent, Optional<AdvancementDisplay> displayInfo, AdvancementRequirements requirements, boolean sendsTelemetryEvent) {
|
||||
public Advancement(Optional<Key> parent, Optional<AdvancementDisplay<I>> displayInfo, AdvancementRequirements requirements, boolean sendsTelemetryEvent) {
|
||||
this.criteria = null;
|
||||
this.displayInfo = displayInfo;
|
||||
this.parent = parent;
|
||||
@@ -28,7 +30,7 @@ public class Advancement {
|
||||
}
|
||||
|
||||
@ApiStatus.Obsolete
|
||||
public Advancement(Optional<Key> parent, Optional<AdvancementDisplay> displayInfo, Map<String, Void> criteria, AdvancementRequirements requirements, boolean sendsTelemetryEvent) {
|
||||
public Advancement(Optional<Key> parent, Optional<AdvancementDisplay<I>> displayInfo, Map<String, Void> criteria, AdvancementRequirements requirements, boolean sendsTelemetryEvent) {
|
||||
this.criteria = criteria;
|
||||
this.displayInfo = displayInfo;
|
||||
this.parent = parent;
|
||||
@@ -36,24 +38,24 @@ public class Advancement {
|
||||
this.sendsTelemetryEvent = sendsTelemetryEvent;
|
||||
}
|
||||
|
||||
public static Advancement read(FriendlyByteBuf buf) {
|
||||
public static <I> Advancement<I> read(FriendlyByteBuf buf, FriendlyByteBuf.Reader<Item<I>> reader) {
|
||||
Optional<Key> parent = buf.readOptional(FriendlyByteBuf::readKey);
|
||||
Optional<AdvancementDisplay> displayInfo = buf.readOptional(byteBuf -> AdvancementDisplay.read(buf));
|
||||
Optional<AdvancementDisplay<I>> displayInfo = buf.readOptional(byteBuf -> AdvancementDisplay.read(buf, reader));
|
||||
if (VersionHelper.isOrAbove1_20_2()) {
|
||||
AdvancementRequirements requirements = AdvancementRequirements.read(buf);
|
||||
boolean sendsTelemetryEvent = buf.readBoolean();
|
||||
return new Advancement(parent, displayInfo, requirements, sendsTelemetryEvent);
|
||||
return new Advancement<>(parent, displayInfo, requirements, sendsTelemetryEvent);
|
||||
} else {
|
||||
Map<String, Void> criteria = buf.readMap(FriendlyByteBuf::readUtf, (byteBuf -> null));
|
||||
AdvancementRequirements requirements = AdvancementRequirements.read(buf);
|
||||
boolean sendsTelemetryEvent = buf.readBoolean();
|
||||
return new Advancement(parent, displayInfo, criteria, requirements, sendsTelemetryEvent);
|
||||
return new Advancement<>(parent, displayInfo, criteria, requirements, sendsTelemetryEvent);
|
||||
}
|
||||
}
|
||||
|
||||
public void write(FriendlyByteBuf buf) {
|
||||
public void write(FriendlyByteBuf buf, FriendlyByteBuf.Writer<Item<I>> writer) {
|
||||
buf.writeOptional(this.parent, FriendlyByteBuf::writeKey);
|
||||
buf.writeOptional(this.displayInfo, (byteBuf, info) -> info.write(buf));
|
||||
buf.writeOptional(this.displayInfo, (byteBuf, info) -> info.write(buf, writer));
|
||||
if (!VersionHelper.isOrAbove1_20_2()) {
|
||||
buf.writeMap(this.criteria, FriendlyByteBuf::writeUtf, ((byteBuf, unused) -> {}));
|
||||
}
|
||||
@@ -61,7 +63,11 @@ public class Advancement {
|
||||
buf.writeBoolean(this.sendsTelemetryEvent);
|
||||
}
|
||||
|
||||
public void applyClientboundData(Player player) {
|
||||
this.displayInfo.ifPresent(info -> info.applyClientboundData(player));
|
||||
public void applyClientboundData(Function<Item<I>, Item<I>> function) {
|
||||
this.displayInfo.ifPresent(info -> info.applyClientboundData(function));
|
||||
}
|
||||
|
||||
public void replaceNetworkTags(Function<Component, Component> function) {
|
||||
this.displayInfo.ifPresent(info -> info.replaceNetworkTags(function));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,26 +2,20 @@ package net.momirealms.craftengine.core.advancement.network;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.momirealms.craftengine.core.advancement.AdvancementType;
|
||||
import net.momirealms.craftengine.core.entity.player.Player;
|
||||
import net.momirealms.craftengine.core.item.Item;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.plugin.config.Config;
|
||||
import net.momirealms.craftengine.core.plugin.context.NetworkTextReplaceContext;
|
||||
import net.momirealms.craftengine.core.plugin.text.component.ComponentProvider;
|
||||
import net.momirealms.craftengine.core.util.AdventureHelper;
|
||||
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class AdvancementDisplay {
|
||||
public class AdvancementDisplay<I> {
|
||||
public static final int FLAG_BACKGROUND = 0b001;
|
||||
public static final int FLAG_SHOW_TOAST = 0b010;
|
||||
public static final int FLAG_HIDDEN = 0b100;
|
||||
private Component title;
|
||||
private Component description;
|
||||
private Item<Object> icon;
|
||||
private Item<I> icon;
|
||||
private Optional<Key> background;
|
||||
private final AdvancementType type;
|
||||
private final boolean showToast;
|
||||
@@ -31,7 +25,7 @@ public class AdvancementDisplay {
|
||||
|
||||
public AdvancementDisplay(Component title,
|
||||
Component description,
|
||||
Item<Object> icon,
|
||||
Item<I> icon,
|
||||
Optional<Key> background,
|
||||
AdvancementType type,
|
||||
boolean showToast,
|
||||
@@ -49,24 +43,19 @@ public class AdvancementDisplay {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public void applyClientboundData(Player player) {
|
||||
this.icon = CraftEngine.instance().itemManager().s2c(this.icon, player);
|
||||
if (Config.interceptAdvancement()) {
|
||||
Map<String, ComponentProvider> tokens1 = CraftEngine.instance().fontManager().matchTags(AdventureHelper.componentToJson(this.title));
|
||||
if (!tokens1.isEmpty()) {
|
||||
this.title = AdventureHelper.replaceText(this.title, tokens1, NetworkTextReplaceContext.of(player));
|
||||
}
|
||||
Map<String, ComponentProvider> tokens2 = CraftEngine.instance().fontManager().matchTags(AdventureHelper.componentToJson(this.description));
|
||||
if (!tokens2.isEmpty()) {
|
||||
this.description = AdventureHelper.replaceText(this.description, tokens2, NetworkTextReplaceContext.of(player));
|
||||
}
|
||||
}
|
||||
public void applyClientboundData(Function<Item<I>, Item<I>> function) {
|
||||
this.icon = function.apply(this.icon);
|
||||
}
|
||||
|
||||
public void write(FriendlyByteBuf buf) {
|
||||
public void replaceNetworkTags(Function<Component, Component> function) {
|
||||
this.title = function.apply(this.title);
|
||||
this.description = function.apply(this.description);
|
||||
}
|
||||
|
||||
public void write(FriendlyByteBuf buf, FriendlyByteBuf.Writer<Item<I>> writer) {
|
||||
buf.writeComponent(this.title);
|
||||
buf.writeComponent(this.description);
|
||||
CraftEngine.instance().itemManager().encode(buf, this.icon);
|
||||
writer.accept(buf, this.icon);
|
||||
buf.writeVarInt(this.type.ordinal());
|
||||
int flags = 0;
|
||||
if (this.background.isPresent()) {
|
||||
@@ -84,10 +73,10 @@ public class AdvancementDisplay {
|
||||
buf.writeFloat(this.y);
|
||||
}
|
||||
|
||||
public static AdvancementDisplay read(FriendlyByteBuf buf) {
|
||||
public static <I> AdvancementDisplay<I> read(FriendlyByteBuf buf, FriendlyByteBuf.Reader<Item<I>> reader) {
|
||||
Component title = buf.readComponent();
|
||||
Component description = buf.readComponent();
|
||||
Item<Object> icon = CraftEngine.instance().itemManager().decode(buf);
|
||||
Item<I> icon = reader.apply(buf);
|
||||
AdvancementType type = AdvancementType.byId(buf.readVarInt());
|
||||
int flags = buf.readInt();
|
||||
boolean hasBackground = (flags & 1) != 0;
|
||||
@@ -96,6 +85,6 @@ public class AdvancementDisplay {
|
||||
boolean hidden = (flags & 4) != 0;
|
||||
float x = buf.readFloat();
|
||||
float y = buf.readFloat();
|
||||
return new AdvancementDisplay(title, description, icon, background, type, showToast, hidden, x, y);
|
||||
return new AdvancementDisplay<>(title, description, icon, background, type, showToast, hidden, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,30 @@
|
||||
package net.momirealms.craftengine.core.advancement.network;
|
||||
|
||||
import net.momirealms.craftengine.core.entity.player.Player;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.momirealms.craftengine.core.item.Item;
|
||||
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
|
||||
public record AdvancementHolder(Key id, Advancement advancement) {
|
||||
import java.util.function.Function;
|
||||
|
||||
public static AdvancementHolder read(FriendlyByteBuf buf) {
|
||||
public record AdvancementHolder<I>(Key id, Advancement<I> advancement) {
|
||||
|
||||
public static <I> AdvancementHolder<I> read(FriendlyByteBuf buf, FriendlyByteBuf.Reader<Item<I>> reader) {
|
||||
Key key = buf.readKey();
|
||||
Advancement ad = Advancement.read(buf);
|
||||
return new AdvancementHolder(key, ad);
|
||||
Advancement<I> ad = Advancement.read(buf, reader);
|
||||
return new AdvancementHolder<>(key, ad);
|
||||
}
|
||||
|
||||
public void write(FriendlyByteBuf buf) {
|
||||
public void write(FriendlyByteBuf buf, FriendlyByteBuf.Writer<Item<I>> writer) {
|
||||
buf.writeKey(this.id);
|
||||
this.advancement.write(buf);
|
||||
this.advancement.write(buf, writer);
|
||||
}
|
||||
|
||||
public void applyClientboundData(Player player) {
|
||||
this.advancement.applyClientboundData(player);
|
||||
public void applyClientboundData(Function<Item<I>, Item<I>> function) {
|
||||
this.advancement.applyClientboundData(function);
|
||||
}
|
||||
|
||||
public void replaceNetworkTags(Function<Component, Component> function) {
|
||||
this.advancement.replaceNetworkTags(function);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,15 +110,9 @@ public interface ItemManager<T> extends Manageable, ModelGenerator {
|
||||
|
||||
boolean isVanillaItem(Key item);
|
||||
|
||||
Item<T> decode(FriendlyByteBuf byteBuf);
|
||||
Optional<Item<T>> c2s(Item<T> item);
|
||||
|
||||
void encode(FriendlyByteBuf byteBuf, Item<T> item);
|
||||
|
||||
Item<T> s2c(Item<T> item, Player player);
|
||||
|
||||
Item<T> c2s(Item<T> item);
|
||||
|
||||
Optional<Item<T>> s2cNew(Item<T> item, Player player);
|
||||
Optional<Item<T>> s2c(Item<T> item, Player player);
|
||||
|
||||
UniqueIdItem<T> uniqueEmptyItem();
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ public class LegacyShapedRecipe<I> implements LegacyRecipe<I> {
|
||||
for (int i = 0; i < size; i++) {
|
||||
ingredients.add(LegacyIngredient.read(buf, reader));
|
||||
}
|
||||
Item<Object> result = CraftEngine.instance().itemManager().decode(buf);
|
||||
Item<I> result = reader.apply(buf);
|
||||
boolean flag = buf.readBoolean();
|
||||
return new LegacyShapedRecipe(width, height, ingredients, result, group, CraftingRecipeCategory.byId(category), flag);
|
||||
} else {
|
||||
@@ -69,7 +69,7 @@ public class LegacyShapedRecipe<I> implements LegacyRecipe<I> {
|
||||
for (int i = 0; i < size; i++) {
|
||||
ingredients.add(LegacyIngredient.read(buf, reader));
|
||||
}
|
||||
Item<Object> result = CraftEngine.instance().itemManager().decode(buf);
|
||||
Item<I> result = reader.apply(buf);
|
||||
boolean flag = buf.readBoolean();
|
||||
return new LegacyShapedRecipe(width, height, ingredients, result, group, CraftingRecipeCategory.byId(category), flag);
|
||||
}
|
||||
|
||||
@@ -27,13 +27,12 @@ public class LegacyShapelessRecipe<I> implements LegacyRecipe<I> {
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public static <I> LegacyShapelessRecipe<I> read(FriendlyByteBuf buf, FriendlyByteBuf.Reader<Item<I>> reader) {
|
||||
String group = buf.readUtf();
|
||||
CraftingRecipeCategory category = CraftingRecipeCategory.byId(buf.readVarInt());
|
||||
List<LegacyIngredient<I>> ingredient = buf.readCollection(ArrayList::new, (byteBuffer) -> LegacyIngredient.read(byteBuffer, reader));
|
||||
Item<Object> result = CraftEngine.instance().itemManager().decode(buf);
|
||||
return new LegacyShapelessRecipe(ingredient, result, group, category);
|
||||
Item<I> result = reader.apply(buf);
|
||||
return new LegacyShapelessRecipe<>(ingredient, result, group, category);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -37,12 +37,11 @@ public class LegacySmithingTransformRecipe<I> implements LegacyRecipe<I> {
|
||||
this.addition.applyClientboundData(function);
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public static <I> LegacySmithingTransformRecipe<I> read(FriendlyByteBuf buf, FriendlyByteBuf.Reader<Item<I>> reader) {
|
||||
LegacyIngredient<I> template = LegacyIngredient.read(buf, reader);
|
||||
LegacyIngredient<I> base = LegacyIngredient.read(buf, reader);
|
||||
LegacyIngredient<I> addition = LegacyIngredient.read(buf, reader);
|
||||
Item<Object> result = CraftEngine.instance().itemManager().decode(buf);
|
||||
return new LegacySmithingTransformRecipe(template, base, addition, result);
|
||||
Item<I> result = reader.apply(buf);
|
||||
return new LegacySmithingTransformRecipe<>(template, base, addition, result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user