Skip to content

Commit

Permalink
Backport sync, hopefully fix quark issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadows-of-Fire committed Oct 15, 2020
1 parent 5212e9b commit 8b798fa
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ build
# other
eclipse
run
logs
28 changes: 13 additions & 15 deletions src/main/java/shadows/apotheosis/Apotheosis.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
16 changes: 0 additions & 16 deletions src/main/java/shadows/apotheosis/deadly/loot/LootEntry.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,9 +35,7 @@
*/
public class LootManager extends JsonReloadListener {

public static final Gson GSON = new GsonBuilder().registerTypeAdapter(LootEntry.class, (JsonDeserializer<LootEntry>) (json, type, ctx) -> {
return LootEntry.deserialize(json.getAsJsonObject());
}).setPrettyPrinting().create();
public static final Gson GSON = BossArmorManager.GSON;

public static final LootManager INSTANCE = new LootManager();

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/shadows/apotheosis/ench/EnchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -45,6 +55,8 @@ protected void apply(Map<ResourceLocation, JsonObject> 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) {
Expand Down Expand Up @@ -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;

Expand All @@ -85,4 +101,51 @@ public Stats(float maxEterna, float eterna, float quanta, float arcana) {
}
}

public static class StatSyncMessage extends MessageProvider<StatSyncMessage> {

final Map<IRegistryDelegate<Block>, Stats> stats;

private StatSyncMessage(Map<IRegistryDelegate<Block>, 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<IRegistryDelegate<Block>, Stats> e : msg.stats.entrySet()) {
buf.writeInt(((ForgeRegistry<Block>) 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<Block>) 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<Context> ctx) {
NetworkUtils.handlePacket(() -> () -> {
INSTANCE.stats.clear();
INSTANCE.stats.putAll(msg.stats);
}, ctx.get());
}

}

}

0 comments on commit 8b798fa

Please sign in to comment.