mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-02-03 04:39:12 +00:00
Make our registry wrapper implement IdMap
This allows us to use some of the byte-buffer helper methods directly with our registries, rather than rolling our own helpers.
This commit is contained in:
parent
eef05b9854
commit
1b88213eca
@ -44,12 +44,12 @@ public class ArgumentUtils {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <A extends ArgumentType<?>, T extends ArgumentTypeInfo.Template<A>> void serializeToNetwork(FriendlyByteBuf buffer, ArgumentTypeInfo<A, T> type, ArgumentTypeInfo.Template<A> template) {
|
||||
RegistryWrappers.writeId(buffer, RegistryWrappers.COMMAND_ARGUMENT_TYPES, type);
|
||||
buffer.writeId(RegistryWrappers.COMMAND_ARGUMENT_TYPES, type);
|
||||
type.serializeToNetwork((T) template, buffer);
|
||||
}
|
||||
|
||||
public static ArgumentTypeInfo.Template<?> deserialize(FriendlyByteBuf buffer) {
|
||||
var type = RegistryWrappers.readId(buffer, RegistryWrappers.COMMAND_ARGUMENT_TYPES);
|
||||
var type = buffer.readById(RegistryWrappers.COMMAND_ARGUMENT_TYPES);
|
||||
Objects.requireNonNull(type, "Unknown argument type");
|
||||
return type.deserializeFromNetwork(buffer);
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
package dan200.computercraft.shared.platform;
|
||||
|
||||
import net.minecraft.commands.synchronization.ArgumentTypeInfo;
|
||||
import net.minecraft.core.IdMap;
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
@ -36,9 +37,7 @@ public final class RegistryWrappers {
|
||||
public static final RegistryWrapper<RecipeSerializer<?>> RECIPE_SERIALIZERS = PlatformHelper.get().wrap(Registries.RECIPE_SERIALIZER);
|
||||
public static final RegistryWrapper<MenuType<?>> MENU = PlatformHelper.get().wrap(Registries.MENU);
|
||||
|
||||
public interface RegistryWrapper<T> extends Iterable<T> {
|
||||
int getId(T object);
|
||||
|
||||
public interface RegistryWrapper<T> extends IdMap<T> {
|
||||
ResourceLocation getKey(T object);
|
||||
|
||||
T get(ResourceLocation location);
|
||||
@ -46,8 +45,6 @@ public final class RegistryWrappers {
|
||||
@Nullable
|
||||
T tryGet(ResourceLocation location);
|
||||
|
||||
T get(int id);
|
||||
|
||||
default Stream<T> stream() {
|
||||
return StreamSupport.stream(spliterator(), false);
|
||||
}
|
||||
@ -56,15 +53,6 @@ public final class RegistryWrappers {
|
||||
private RegistryWrappers() {
|
||||
}
|
||||
|
||||
public static <K> void writeId(FriendlyByteBuf buf, RegistryWrapper<K> registry, K object) {
|
||||
buf.writeVarInt(registry.getId(object));
|
||||
}
|
||||
|
||||
public static <K> K readId(FriendlyByteBuf buf, RegistryWrapper<K> registry) {
|
||||
var id = buf.readVarInt();
|
||||
return registry.get(id);
|
||||
}
|
||||
|
||||
public static <K> void writeKey(FriendlyByteBuf buf, RegistryWrapper<K> registry, K object) {
|
||||
buf.writeResourceLocation(registry.getKey(object));
|
||||
}
|
||||
|
@ -17,6 +17,8 @@ import net.minecraft.util.GsonHelper;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public final class TurtleToolSerialiser implements TurtleUpgradeSerialiser<TurtleTool> {
|
||||
public static final TurtleToolSerialiser INSTANCE = new TurtleToolSerialiser();
|
||||
|
||||
@ -44,7 +46,8 @@ public final class TurtleToolSerialiser implements TurtleUpgradeSerialiser<Turtl
|
||||
@Override
|
||||
public TurtleTool fromNetwork(ResourceLocation id, FriendlyByteBuf buffer) {
|
||||
var adjective = buffer.readUtf();
|
||||
var craftingItem = RegistryWrappers.readId(buffer, RegistryWrappers.ITEMS);
|
||||
var craftingItem = buffer.readById(RegistryWrappers.ITEMS);
|
||||
Objects.requireNonNull(craftingItem, "Unknown crafting item");
|
||||
var toolItem = buffer.readItem();
|
||||
// damageMultiplier and breakable aren't used by the client, but we need to construct the upgrade exactly
|
||||
// as otherwise syncing on an SP world will overwrite the (shared) upgrade registry with an invalid upgrade!
|
||||
@ -59,7 +62,7 @@ public final class TurtleToolSerialiser implements TurtleUpgradeSerialiser<Turtl
|
||||
@Override
|
||||
public void toNetwork(FriendlyByteBuf buffer, TurtleTool upgrade) {
|
||||
buffer.writeUtf(upgrade.getUnlocalisedAdjective());
|
||||
RegistryWrappers.writeId(buffer, RegistryWrappers.ITEMS, upgrade.getCraftingItem().getItem());
|
||||
buffer.writeId(RegistryWrappers.ITEMS, upgrade.getCraftingItem().getItem());
|
||||
buffer.writeItem(upgrade.item);
|
||||
buffer.writeFloat(upgrade.damageMulitiplier);
|
||||
buffer.writeBoolean(upgrade.allowEnchantments);
|
||||
|
@ -303,7 +303,7 @@ public class PlatformHelperImpl implements PlatformHelper {
|
||||
public boolean hasToolUsage(ItemStack stack) {
|
||||
var item = stack.getItem();
|
||||
return item instanceof ShovelItem || stack.is(ItemTags.SHOVELS) ||
|
||||
item instanceof HoeItem || stack.is(ItemTags.HOES);
|
||||
item instanceof HoeItem || stack.is(ItemTags.HOES);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -314,8 +314,8 @@ public class PlatformHelperImpl implements PlatformHelper {
|
||||
@Override
|
||||
public boolean interactWithEntity(ServerPlayer player, Entity entity, Vec3 hitPos) {
|
||||
return UseEntityCallback.EVENT.invoker().interact(player, entity.level, InteractionHand.MAIN_HAND, entity, new EntityHitResult(entity, hitPos)).consumesAction() ||
|
||||
entity.interactAt(player, hitPos.subtract(entity.position()), InteractionHand.MAIN_HAND).consumesAction() ||
|
||||
player.interactOn(entity, InteractionHand.MAIN_HAND).consumesAction();
|
||||
entity.interactAt(player, hitPos.subtract(entity.position()), InteractionHand.MAIN_HAND).consumesAction() ||
|
||||
player.interactOn(entity, InteractionHand.MAIN_HAND).consumesAction();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -337,9 +337,7 @@ public class PlatformHelperImpl implements PlatformHelper {
|
||||
) implements RegistryWrappers.RegistryWrapper<T> {
|
||||
@Override
|
||||
public int getId(T object) {
|
||||
var id = registry.getId(object);
|
||||
if (id == -1) throw new IllegalArgumentException(object + " was not registered in " + name);
|
||||
return id;
|
||||
return registry.getId(object);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -363,10 +361,13 @@ public class PlatformHelperImpl implements PlatformHelper {
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(int id) {
|
||||
var object = registry.byId(id);
|
||||
if (object == null) throw new IllegalArgumentException(id + " was not registered in " + name);
|
||||
return object;
|
||||
public @Nullable T byId(int id) {
|
||||
return registry.byId(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return registry.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -359,9 +359,7 @@ public class PlatformHelperImpl implements PlatformHelper {
|
||||
) implements RegistryWrappers.RegistryWrapper<T> {
|
||||
@Override
|
||||
public int getId(T object) {
|
||||
var id = registry.getID(object);
|
||||
if (id == -1) throw new IllegalStateException(object + " was not registered in " + name);
|
||||
return id;
|
||||
return registry.getID(object);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -385,10 +383,13 @@ public class PlatformHelperImpl implements PlatformHelper {
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(int id) {
|
||||
var object = registry.getValue(id);
|
||||
if (object == null) throw new IllegalStateException(id + " was not registered in " + name);
|
||||
return object;
|
||||
public @Nullable T byId(int id) {
|
||||
return registry.getValue(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return registry.getKeys().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user