Skip to content

Commit

Permalink
Add resource reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
Dueris committed Aug 31, 2024
1 parent bca1829 commit 321e7ed
Show file tree
Hide file tree
Showing 27 changed files with 114 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public <T> Registrar<T> create(RegistryKey<T> key, Registrar<T> registrar) {

@Override
public void clearRegistries() {
this.registry.values().forEach(Registrar::clearEntries);
this.registry.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
public class Bootstrap implements PluginBootstrap {

@Override
@SuppressWarnings("unchecked")
public void bootstrap(@Nullable BootstrapContext bootContext) {
WrappedBootstrapContext context = new WrappedBootstrapContext(bootContext);
context.registerBuiltin(BuiltInRegistries.LOOT_CONDITION_TYPE, ResourceLocation.fromNamespaceAndPath("apoli", "power"), PowerLootCondition.TYPE);
context.registerBuiltin(BuiltInRegistries.LOOT_CONDITION_TYPE, ResourceLocation.fromNamespaceAndPath("origins", "origin"), OriginLootCondition.TYPE);

try {
// ((PluginProvider)((LinkedList)LaunchEntryPointHandler.INSTANCE.get(Entrypoint.BOOTSTRAPPER).getRegisteredProviders()).getFirst()).createInstance();
OriginsPaper.init(context);
} catch (Throwable e) {
throw new RuntimeException("An error occurred when loading OriginsPaper!", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import io.github.dueris.originspaper.origin.Origin;
import io.github.dueris.originspaper.origin.OriginLayer;
import io.github.dueris.originspaper.plugin.OriginsPlugin;
import io.github.dueris.originspaper.power.type.FireProjectilePower;
import io.github.dueris.originspaper.power.factory.PowerType;
import io.github.dueris.originspaper.power.type.FireProjectilePower;
import io.github.dueris.originspaper.registry.Registries;
import io.github.dueris.originspaper.storage.OriginConfiguration;
import io.github.dueris.originspaper.util.LangFile;
Expand Down Expand Up @@ -131,6 +131,11 @@ public static void init(@NotNull WrappedBootstrapContext context) throws Throwab
(tuple) -> FireProjectilePower.IS_ENDERIAN_PEARL.add(tuple.getB())
));
CalioParser.REMAPPER.set(remapper);
reload();
ApiCall.call(ApiCall.PRE_PARSE, context);
}

public static void reload() throws Throwable {
context.createRegistries(
Registries.ORIGIN,
Registries.LAYER,
Expand Down Expand Up @@ -172,8 +177,6 @@ public static void init(@NotNull WrappedBootstrapContext context) throws Throwab
ItemActions.register();
BlockActions.register();
BiEntityActions.register();

ApiCall.call(ApiCall.PRE_PARSE, context);
}

public enum ApiCall {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import io.github.dueris.originspaper.action.factory.ActionTypeFactory;
import io.github.dueris.originspaper.data.ApoliDataTypes;
import io.github.dueris.originspaper.data.types.ResourceOperation;
import io.github.dueris.originspaper.power.factory.PowerReference;
import io.github.dueris.originspaper.power.type.CooldownPower;
import io.github.dueris.originspaper.power.type.ResourcePower;
import io.github.dueris.originspaper.power.factory.PowerReference;
import net.minecraft.world.entity.Entity;
import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import io.github.dueris.originspaper.action.factory.ActionTypeFactory;
import io.github.dueris.originspaper.data.ApoliDataTypes;
import io.github.dueris.originspaper.data.types.modifier.Modifier;
import io.github.dueris.originspaper.power.factory.PowerReference;
import io.github.dueris.originspaper.power.type.CooldownPower;
import io.github.dueris.originspaper.power.type.ResourcePower;
import io.github.dueris.originspaper.power.factory.PowerReference;
import net.minecraft.world.entity.Entity;
import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import io.github.dueris.originspaper.OriginsPaper;
import io.github.dueris.originspaper.action.factory.ActionTypeFactory;
import io.github.dueris.originspaper.data.ApoliDataTypes;
import io.github.dueris.originspaper.power.factory.PowerReference;
import io.github.dueris.originspaper.power.type.CooldownPower;
import io.github.dueris.originspaper.power.type.ResourcePower;
import io.github.dueris.originspaper.power.factory.PowerReference;
import net.minecraft.world.entity.Entity;
import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import io.github.dueris.originspaper.action.factory.ActionTypeFactory;
import io.github.dueris.originspaper.data.ApoliDataTypes;
import io.github.dueris.originspaper.event.KeybindTriggerEvent;
import io.github.dueris.originspaper.power.type.TogglePower;
import io.github.dueris.originspaper.power.factory.PowerReference;
import io.github.dueris.originspaper.power.type.TogglePower;
import net.minecraft.world.entity.Entity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import io.github.dueris.originspaper.OriginsPaper;
import io.github.dueris.originspaper.action.factory.ActionTypeFactory;
import io.github.dueris.originspaper.data.ApoliDataTypes;
import io.github.dueris.originspaper.power.type.CooldownPower;
import io.github.dueris.originspaper.power.factory.PowerReference;
import io.github.dueris.originspaper.power.type.CooldownPower;
import net.minecraft.world.entity.Entity;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import io.github.dueris.originspaper.command.argument.PowerArgumentType;
import io.github.dueris.originspaper.command.argument.PowerHolderArgumentType;
import io.github.dueris.originspaper.origin.OriginLayer;
import io.github.dueris.originspaper.power.type.MultiplePower;
import io.github.dueris.originspaper.power.factory.PowerType;
import io.github.dueris.originspaper.power.type.MultiplePower;
import io.github.dueris.originspaper.storage.PowerHolderComponent;
import io.github.dueris.originspaper.util.JsonTextFormatter;
import io.github.dueris.originspaper.util.LangFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import io.github.dueris.originspaper.command.argument.PowerArgumentType;
import io.github.dueris.originspaper.command.argument.PowerHolderArgumentType;
import io.github.dueris.originspaper.command.argument.PowerOperationArgumentType;
import io.github.dueris.originspaper.power.factory.PowerType;
import io.github.dueris.originspaper.power.type.CooldownPower;
import io.github.dueris.originspaper.power.type.ResourcePower;
import io.github.dueris.originspaper.power.factory.PowerType;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import net.minecraft.commands.arguments.EntityArgument;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import io.github.dueris.originspaper.power.factory.PowerType;
import io.github.dueris.originspaper.power.type.CooldownPower;
import io.github.dueris.originspaper.power.type.ResourcePower;
import io.github.dueris.originspaper.power.factory.PowerType;
import io.papermc.paper.command.brigadier.argument.CustomArgumentType;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.network.chat.Component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import io.github.dueris.originspaper.condition.factory.ConditionTypeFactory;
import io.github.dueris.originspaper.data.ApoliDataTypes;
import io.github.dueris.originspaper.data.types.Comparison;
import io.github.dueris.originspaper.power.factory.PowerReference;
import io.github.dueris.originspaper.power.type.CooldownPower;
import io.github.dueris.originspaper.power.type.ResourcePower;
import io.github.dueris.originspaper.power.factory.PowerReference;
import net.minecraft.world.entity.Entity;
import org.jetbrains.annotations.NotNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void loadPacks(@NotNull Consumer<Pack> profileAdder) {

private @NotNull PackLocationInfo createDiscoveredFilePackInfo(Path path) {
String string = nameFromPath(path);
return new PackLocationInfo("assets/origins/" + string, Component.literal(string), this.packSource, Optional.empty());
return new PackLocationInfo("origins/" + string, Component.literal(string), this.packSource, Optional.empty());
}

static class FolderPackDetector extends PackDetector<Pack.ResourcesSupplier> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.ItemEnchantments;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.ApiStatus;

public final class At {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.dragoncommissions.mixbukkit.api.shellcode.impl.api.CallbackInfo;
import io.github.dueris.originspaper.data.types.modifier.ModifierUtil;
import io.github.dueris.originspaper.power.type.simple.WaterBreathingPower;
import io.github.dueris.originspaper.power.type.*;
import io.github.dueris.originspaper.power.type.simple.WaterBreathingPower;
import io.github.dueris.originspaper.storage.PowerHolderComponent;
import net.minecraft.core.Holder;
import net.minecraft.tags.EntityTypeTags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@
import com.dragoncommissions.mixbukkit.api.shellcode.impl.api.CallbackInfo;
import io.github.dueris.calio.CraftCalio;
import io.github.dueris.calio.data.DataBuildDirective;
import io.github.dueris.calio.registry.impl.CalioRegistry;
import io.github.dueris.originspaper.OriginsPaper;
import io.github.dueris.originspaper.origin.Origin;
import io.github.dueris.originspaper.origin.OriginLayer;
import io.github.dueris.originspaper.power.factory.PowerTypeFactory;
import io.github.dueris.originspaper.power.type.RecipePower;
import io.github.dueris.originspaper.registry.BuiltinRegistry;
import io.github.dueris.originspaper.registry.Registries;
import io.github.dueris.originspaper.storage.PlayerPowerRepository;
import io.github.dueris.originspaper.storage.PowerHolderComponent;
import io.github.dueris.originspaper.util.ApoliScheduler;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.List;
import java.util.function.BooleanSupplier;

Expand All @@ -29,17 +39,68 @@ public static void tickHook(MinecraftServer server, BooleanSupplier shouldKeepTi

@Inject(method = "runServer", locator = At.Value.HEAD)
public static void originspaper$init(@NotNull MinecraftServer server, CallbackInfo info) {
CraftCalio calio = CraftCalio.buildInstance().startBuilder()
.withAccessor(new DataBuildDirective<>(List.of(), "powers", PowerTypeFactory.DATA, 0, Registries.POWER))
.withAccessor(new DataBuildDirective<>(List.of(), "origins", Origin.DATA, 1, Registries.ORIGIN))
.withAccessor(new DataBuildDirective<>(List.of(), "origin_layers", OriginLayer.DATA, 2, Registries.LAYER))
.build();
CraftCalio calio = buildNewCalio();

parse(server, calio);

BuiltinRegistry.bootstrap();
int[] registrySizes = new int[]{
OriginsPaper.getRegistry().retrieve(Registries.POWER).registrySize(), OriginsPaper.getRegistry().retrieve(Registries.ORIGIN).registrySize(), OriginsPaper.getRegistry().retrieve(Registries.LAYER).registrySize()
};
OriginsPaper.LOGGER.info("Registry contains {} powers, {} origins, and {} layers.", registrySizes[0], registrySizes[1], registrySizes[2]);
OriginsPaper.LOGGER.info("OriginsPaper {} has initialized. Ready to power up your game!", OriginsPaper.pluginData.getFullVersion());

calio.shutdown();
}

@Inject(method = "reloadResources", locator = At.Value.HEAD)
public static void calio$reload(@NotNull MinecraftServer server, Collection<String> dataPacks, io.papermc.paper.event.server.ServerResourcesReloadedEvent.Cause cause, CallbackInfo info) {
CraftCalio calio = buildNewCalio();
CalioRegistry.INSTANCE.clearRegistries();
RecipePower.recipeMapping.clear();
RecipePower.tags.clear();
for (ServerPlayer player : server.getPlayerList().players) {
PlayerPowerRepository repository = PlayerPowerRepository.getOrCreateRepo(player);
String saveData = repository.serializePowers(new CompoundTag()).toString();
player.getBukkitEntity().getPersistentDataContainer()
.set(identifier(), PersistentDataType.STRING, saveData);
PowerHolderComponent.unloadPowers(player.getBukkitEntity());
repository.clearData();
}

try {
PlayerPowerRepository.clearRepository();
OriginsPaper.reload();
parse(server, calio);
BuiltinRegistry.bootstrap();

OriginsPaper.getRegistry().retrieve(Registries.POWER).values().forEach(powerType -> {
OriginsPaper.getPlugin().getServer().getPluginManager().registerEvents(powerType, OriginsPaper.getPlugin());
});
} catch (Throwable e) {
throw new RuntimeException("Unable to run calio reload!", e);
}

for (ServerPlayer player : server.getPlayerList().players) {
PlayerPowerRepository repository = PlayerPowerRepository.getOrCreateRepo(player);
repository.readPowers(
player.getBukkitEntity().getPersistentDataContainer()
.get(identifier(), PersistentDataType.STRING)
);

PowerHolderComponent.loadPowers(player.getBukkitEntity());
}

Bukkit.updateRecipes();
}

private static void parse(@NotNull MinecraftServer server, CraftCalio calio) {
PackRepositoryMixin.getAvailable(server.getPackRepository()).forEach((id, pack) -> {
Path packPath = null;
if (id.startsWith("file/")) {
String packName = id.split("file/")[1];
packPath = PackRepositoryMixin.DATAPACK_PATH.get().resolve(packName);
} else if (id.startsWith("assets/origins/")) {
} else if (id.startsWith("origins/")) {
String pluginName = id.split("origins/")[1];
packPath = Paths.get("plugins/").resolve(pluginName);
}
Expand All @@ -48,14 +109,17 @@ public static void tickHook(MinecraftServer server, BooleanSupplier shouldKeepTi
calio.parse(packPath.toAbsolutePath());
}
});
}

BuiltinRegistry.bootstrap();
int[] registrySizes = new int[]{
OriginsPaper.getRegistry().retrieve(Registries.POWER).registrySize(), OriginsPaper.getRegistry().retrieve(Registries.ORIGIN).registrySize(), OriginsPaper.getRegistry().retrieve(Registries.LAYER).registrySize()
};
OriginsPaper.LOGGER.info("Registry contains {} powers, {} origins, and {} layers.", registrySizes[0], registrySizes[1], registrySizes[2]);
OriginsPaper.LOGGER.info("OriginsPaper {} has initialized. Ready to power up your game!", OriginsPaper.pluginData.getFullVersion());
private static @NotNull NamespacedKey identifier() {
return new NamespacedKey(OriginsPaper.getPlugin(), "powers");
}

calio.shutdown();
private static CraftCalio buildNewCalio() {
return CraftCalio.buildInstance().startBuilder()
.withAccessor(new DataBuildDirective<>(List.of(), "powers", PowerTypeFactory.DATA, 0, Registries.POWER))
.withAccessor(new DataBuildDirective<>(List.of(), "origins", Origin.DATA, 1, Registries.ORIGIN))
.withAccessor(new DataBuildDirective<>(List.of(), "origin_layers", OriginLayer.DATA, 2, Registries.LAYER))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class PlayerMixin {

@Inject(method = "stopSleepInBed", locator = At.Value.HEAD)
public static void apoli$actionOnWakeUp(Player instance, boolean bl, boolean updateSleepingPlayers, CallbackInfo info) {
if(!bl && !updateSleepingPlayers && instance.getSleepingPos().isPresent()) {
if (!bl && !updateSleepingPlayers && instance.getSleepingPos().isPresent()) {
BlockPos sleepingPos = instance.getSleepingPos().get();
PowerHolderComponent.getPowers(instance.getBukkitEntity(), ActionOnWakeUpPower.class).stream().filter(p -> p.doesApply(sleepingPos, instance)).forEach(p -> p.executeActions(sleepingPos, Direction.DOWN, instance));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void onDisable() {
player.closeInventory();
player.getPersistentDataContainer()
.set(new NamespacedKey(this, "powers"), PersistentDataType.STRING,
PlayerPowerRepository.getOrCreateRepo(((CraftPlayer) player).getHandle()).serializePowers(new CompoundTag(), ((CraftPlayer) player).getHandle()).toString());
PlayerPowerRepository.getOrCreateRepo(((CraftPlayer) player).getHandle()).serializePowers(new CompoundTag()).toString());
PowerHolderComponent.unloadPowers(player);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.github.dueris.originspaper.data.ApoliDataTypes;
import io.github.dueris.originspaper.power.factory.PowerType;
import io.github.dueris.originspaper.util.Util;
import io.papermc.paper.event.player.PlayerFailMoveEvent;
import net.minecraft.Optionull;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
Expand All @@ -21,7 +20,6 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.block.state.pattern.BlockInWorld;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.jetbrains.annotations.NotNull;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import io.github.dueris.originspaper.data.types.Impact;
import io.github.dueris.originspaper.origin.Origin;
import io.github.dueris.originspaper.origin.OriginLayer;
import io.github.dueris.originspaper.power.type.ModifyPlayerSpawnPower;
import io.github.dueris.originspaper.power.factory.PowerType;
import io.github.dueris.originspaper.power.type.ModifyPlayerSpawnPower;
import io.github.dueris.originspaper.storage.OriginComponent;
import io.github.dueris.originspaper.storage.PowerHolderComponent;
import io.github.dueris.originspaper.util.ComponentUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import io.github.dueris.originspaper.event.OriginChangeEvent;
import io.github.dueris.originspaper.origin.Origin;
import io.github.dueris.originspaper.origin.OriginLayer;
import io.github.dueris.originspaper.power.type.MultiplePower;
import io.github.dueris.originspaper.power.factory.PowerType;
import io.github.dueris.originspaper.power.type.MultiplePower;
import io.github.dueris.originspaper.registry.Registries;
import io.github.dueris.originspaper.screen.ScreenNavigator;
import io.github.dueris.originspaper.util.BstatsMetrics;
Expand Down
Loading

0 comments on commit 321e7ed

Please sign in to comment.