package net.minecraft.world.level.timers; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Maps; import com.mojang.logging.LogUtils; import java.util.Map; import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; public class TimerCallbacks { private static final Logger LOGGER = LogUtils.getLogger(); public static final TimerCallbacks SERVER_CALLBACKS = new TimerCallbacks() .register(new FunctionCallback.Serializer()) .register(new FunctionTagCallback.Serializer()); private final Map> idToSerializer = Maps.>newHashMap(); private final Map, TimerCallback.Serializer> classToSerializer = Maps., TimerCallback.Serializer>newHashMap(); public TimerCallbacks register(TimerCallback.Serializer serializer) { this.idToSerializer.put(serializer.getId(), serializer); this.classToSerializer.put(serializer.getCls(), serializer); return this; } private > TimerCallback.Serializer getSerializer(Class clazz) { return (TimerCallback.Serializer)this.classToSerializer.get(clazz); } public > CompoundTag serialize(T callback) { TimerCallback.Serializer serializer = this.getSerializer(callback.getClass()); CompoundTag compoundTag = new CompoundTag(); serializer.serialize(compoundTag, callback); compoundTag.putString("Type", serializer.getId().toString()); return compoundTag; } @Nullable public TimerCallback deserialize(CompoundTag tag) { ResourceLocation resourceLocation = ResourceLocation.tryParse(tag.getString("Type")); TimerCallback.Serializer serializer = (TimerCallback.Serializer)this.idToSerializer.get(resourceLocation); if (serializer == null) { LOGGER.error("Failed to deserialize timer callback: {}", tag); return null; } else { try { return serializer.deserialize(tag); } catch (Exception var5) { LOGGER.error("Failed to deserialize timer callback: {}", tag, var5); return null; } } } }