From 8b798fa515c2629fbc5635c15bb62a3d20f56256 Mon Sep 17 00:00:00 2001 From: Shadows_of_Fire Date: Wed, 14 Oct 2020 20:47:57 -0400 Subject: [PATCH] Backport sync, hopefully fix quark issues --- .gitignore | 1 + .../java/shadows/apotheosis/Apotheosis.java | 28 ++++----- .../apotheosis/deadly/loot/LootEntry.java | 16 ----- .../apotheosis/deadly/loot/LootManager.java | 6 +- .../shadows/apotheosis/ench/EnchModule.java | 9 +++ .../ench/table/EnchantingStatManager.java | 63 +++++++++++++++++++ 6 files changed, 87 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index 2c770e09f..ea505d143 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ build # other eclipse run +logs \ No newline at end of file diff --git a/src/main/java/shadows/apotheosis/Apotheosis.java b/src/main/java/shadows/apotheosis/Apotheosis.java index fa4e663f7..da9cf1eca 100644 --- a/src/main/java/shadows/apotheosis/Apotheosis.java +++ b/src/main/java/shadows/apotheosis/Apotheosis.java @@ -27,6 +27,7 @@ import shadows.apotheosis.deadly.DeadlyModule; import shadows.apotheosis.deadly.loot.affix.Affix; import shadows.apotheosis.ench.EnchModule; +import shadows.apotheosis.ench.table.EnchantingStatManager.StatSyncMessage; import shadows.apotheosis.garden.GardenModule; import shadows.apotheosis.potion.PotionModule; import shadows.apotheosis.spawn.SpawnerModule; @@ -66,42 +67,39 @@ public class Apotheosis { public static float localAtkStrength = 1; - public Apotheosis() { - Affix.classload(); + static { configDir = new File(FMLPaths.CONFIGDIR.get().toFile(), MODID); config = new Configuration(new File(configDir, MODID + ".cfg")); + enableEnch = config.getBoolean("Enable Enchantment Module", "general", true, "If the enchantment module is enabled."); + enableSpawner = config.getBoolean("Enable Spawner Module", "general", true, "If the spawner module is enabled."); + enableGarden = config.getBoolean("Enable Garden Module", "general", true, "If the garden module is loaded."); + enableDeadly = config.getBoolean("Enable Deadly Module", "general", true, "If the deadly module is loaded."); + enablePotion = config.getBoolean("Enable Potion Module", "general", true, "If the potion module is loaded."); + enableVillager = config.getBoolean("Enable Village Module", "general", enableVillager, "If the village module is loaded."); + if (config.hasChanged()) config.save(); + } + public Apotheosis() { + Affix.classload(); IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus(); - enableEnch = config.getBoolean("Enable Enchantment Module", "general", true, "If the enchantment module is enabled."); if (enableEnch) bus.register(new EnchModule()); - - enableSpawner = config.getBoolean("Enable Spawner Module", "general", true, "If the spawner module is enabled."); if (enableSpawner) bus.register(new SpawnerModule()); - - enableGarden = config.getBoolean("Enable Garden Module", "general", true, "If the garden module is loaded."); if (enableGarden) bus.register(new GardenModule()); - - enableDeadly = config.getBoolean("Enable Deadly Module", "general", true, "If the deadly module is loaded."); if (enableDeadly) bus.register(new DeadlyModule()); - - enablePotion = config.getBoolean("Enable Potion Module", "general", true, "If the potion module is loaded."); if (enablePotion) bus.register(new PotionModule()); - - enableVillager = config.getBoolean("Enable Village Module", "general", enableVillager, "If the village module is loaded."); if (enableVillager) bus.register(new VillageModule()); - if (config.hasChanged()) config.save(); bus.post(new ApotheosisConstruction()); bus.addListener(this::init); bus.addListener(this::initC); MinecraftForge.EVENT_BUS.addListener(this::trackCooldown); - } @SubscribeEvent public void init(FMLCommonSetupEvent e) { NetworkUtils.registerMessage(CHANNEL, 0, new ParticleMessage()); + NetworkUtils.registerMessage(CHANNEL, 1, new StatSyncMessage()); FMLJavaModLoadingContext.get().getModEventBus().post(new ApotheosisSetup()); DeferredWorkQueue.runLater(AdvancementTriggers::init); CraftingHelper.register(new ModuleCondition.Serializer()); diff --git a/src/main/java/shadows/apotheosis/deadly/loot/LootEntry.java b/src/main/java/shadows/apotheosis/deadly/loot/LootEntry.java index 455eb36ca..166b62080 100644 --- a/src/main/java/shadows/apotheosis/deadly/loot/LootEntry.java +++ b/src/main/java/shadows/apotheosis/deadly/loot/LootEntry.java @@ -1,13 +1,7 @@ package shadows.apotheosis.deadly.loot; -import java.util.Locale; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; - import net.minecraft.item.ItemStack; import net.minecraft.util.WeightedRandom; -import net.minecraftforge.common.crafting.CraftingHelper; /** * A loot entry represents a possible item that can come out of a loot roll. @@ -32,14 +26,4 @@ public EquipmentType getType() { return type; } - public static LootEntry deserialize(JsonObject obj) { - JsonElement stack = obj.get("stack"); - JsonElement type = obj.get("type"); - JsonElement weight = obj.get("weight"); - ItemStack _stack = CraftingHelper.getItemStack(stack.getAsJsonObject(), true); - EquipmentType _type = EquipmentType.valueOf(type.getAsString().toUpperCase(Locale.ROOT)); - int _weight = weight.getAsInt(); - return new LootEntry(_stack, _type, _weight); - } - } diff --git a/src/main/java/shadows/apotheosis/deadly/loot/LootManager.java b/src/main/java/shadows/apotheosis/deadly/loot/LootManager.java index 8a125e27f..4de2605ab 100644 --- a/src/main/java/shadows/apotheosis/deadly/loot/LootManager.java +++ b/src/main/java/shadows/apotheosis/deadly/loot/LootManager.java @@ -9,8 +9,6 @@ import com.google.common.collect.Multimap; import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonDeserializer; import com.google.gson.JsonObject; import net.minecraft.client.resources.JsonReloadListener; @@ -37,9 +35,7 @@ */ public class LootManager extends JsonReloadListener { - public static final Gson GSON = new GsonBuilder().registerTypeAdapter(LootEntry.class, (JsonDeserializer) (json, type, ctx) -> { - return LootEntry.deserialize(json.getAsJsonObject()); - }).setPrettyPrinting().create(); + public static final Gson GSON = BossArmorManager.GSON; public static final LootManager INSTANCE = new LootManager(); diff --git a/src/main/java/shadows/apotheosis/ench/EnchModule.java b/src/main/java/shadows/apotheosis/ench/EnchModule.java index a3705e919..93250ab67 100644 --- a/src/main/java/shadows/apotheosis/ench/EnchModule.java +++ b/src/main/java/shadows/apotheosis/ench/EnchModule.java @@ -63,6 +63,7 @@ import net.minecraftforge.event.entity.living.LivingHurtEvent; import net.minecraftforge.event.entity.player.AnvilRepairEvent; import net.minecraftforge.event.entity.player.PlayerEvent; +import net.minecraftforge.event.entity.player.PlayerEvent.PlayerLoggedInEvent; import net.minecraftforge.event.entity.player.PlayerInteractEvent; import net.minecraftforge.eventbus.api.EventPriority; import net.minecraftforge.eventbus.api.SubscribeEvent; @@ -507,6 +508,14 @@ public void livingHurt(LivingHurtEvent e) { } } } + + @SubscribeEvent + public void login(PlayerLoggedInEvent e) { + PlayerEntity p = e.getPlayer(); + if (!p.world.isRemote) { + EnchantingStatManager.dispatch(p); + } + } public static EnchantmentInfo getEnchInfo(Enchantment ench) { EnchantmentInfo info = ENCHANTMENT_INFO.get(ench); diff --git a/src/main/java/shadows/apotheosis/ench/table/EnchantingStatManager.java b/src/main/java/shadows/apotheosis/ench/table/EnchantingStatManager.java index de53f5026..2bb3c2cdb 100644 --- a/src/main/java/shadows/apotheosis/ench/table/EnchantingStatManager.java +++ b/src/main/java/shadows/apotheosis/ench/table/EnchantingStatManager.java @@ -2,6 +2,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.function.Supplier; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -10,15 +11,24 @@ import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.client.resources.JsonReloadListener; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.network.PacketBuffer; import net.minecraft.profiler.IProfiler; import net.minecraft.resources.IResourceManager; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import net.minecraftforge.fml.network.NetworkEvent.Context; +import net.minecraftforge.fml.network.PacketDistributor; +import net.minecraftforge.fml.server.ServerLifecycleHooks; import net.minecraftforge.registries.ForgeRegistries; +import net.minecraftforge.registries.ForgeRegistry; import net.minecraftforge.registries.IRegistryDelegate; +import shadows.apotheosis.Apotheosis; import shadows.apotheosis.ench.EnchModule; import shadows.apotheosis.ench.objects.IEnchantingBlock; +import shadows.placebo.util.NetworkUtils; +import shadows.placebo.util.NetworkUtils.MessageProvider; public class EnchantingStatManager extends JsonReloadListener { @@ -45,6 +55,8 @@ protected void apply(Map objects, IResourceManager e.printStackTrace(); } }); + EnchModule.LOGGER.info("Registered {} blocks with enchanting stats.", stats.size()); + if (ServerLifecycleHooks.getCurrentServer() != null) Apotheosis.CHANNEL.send(PacketDistributor.ALL.noArg(), new StatSyncMessage(this.stats)); } public static float getEterna(BlockState state, World world, BlockPos pos) { @@ -74,6 +86,10 @@ public static float getArcana(BlockState state, World world, BlockPos pos) { return 0; } + public static void dispatch(PlayerEntity player) { + NetworkUtils.sendTo(Apotheosis.CHANNEL, new StatSyncMessage(INSTANCE.stats), player); + } + public static class Stats { final float maxEterna, eterna, quanta, arcana; @@ -85,4 +101,51 @@ public Stats(float maxEterna, float eterna, float quanta, float arcana) { } } + public static class StatSyncMessage extends MessageProvider { + + final Map, Stats> stats; + + private StatSyncMessage(Map, Stats> stats) { + this.stats = stats; + } + + public StatSyncMessage() { + this.stats = new HashMap<>(); + } + + @Override + public void write(StatSyncMessage msg, PacketBuffer buf) { + buf.writeShort(msg.stats.size()); + for (Map.Entry, Stats> e : msg.stats.entrySet()) { + buf.writeInt(((ForgeRegistry) ForgeRegistries.BLOCKS).getID(e.getKey().get())); + Stats stat = e.getValue(); + buf.writeFloat(stat.maxEterna); + buf.writeFloat(stat.eterna); + buf.writeFloat(stat.quanta); + buf.writeFloat(stat.arcana); + } + } + + @Override + public StatSyncMessage read(PacketBuffer buf) { + int size = buf.readShort(); + StatSyncMessage pkt = new StatSyncMessage(); + for (int i = 0; i < size; i++) { + Block b = ((ForgeRegistry) ForgeRegistries.BLOCKS).getValue(buf.readInt()); + Stats stats = new Stats(buf.readFloat(), buf.readFloat(), buf.readFloat(), buf.readFloat()); + pkt.stats.put(b.delegate, stats); + } + return pkt; + } + + @Override + public void handle(StatSyncMessage msg, Supplier ctx) { + NetworkUtils.handlePacket(() -> () -> { + INSTANCE.stats.clear(); + INSTANCE.stats.putAll(msg.stats); + }, ctx.get()); + } + + } + }