Skip to content

Commit

Permalink
ModusHolder attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
kirderf1 committed Apr 20, 2024
1 parent 9d5eb62 commit 5fb11fc
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 102 deletions.
16 changes: 8 additions & 8 deletions src/main/java/com/mraof/minestuck/event/ServerEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
import com.mraof.minestuck.effects.MSEffects;
import com.mraof.minestuck.entity.underling.UnderlingEntity;
import com.mraof.minestuck.entry.EntryEvent;
import com.mraof.minestuck.inventory.captchalogue.CaptchaDeckHandler;
import com.mraof.minestuck.inventory.captchalogue.HashMapModus;
import com.mraof.minestuck.inventory.captchalogue.Modus;
import com.mraof.minestuck.item.MSItems;
import com.mraof.minestuck.player.*;
import com.mraof.minestuck.player.Echeladder;
import com.mraof.minestuck.player.EnumAspect;
import com.mraof.minestuck.player.IdentifierHandler;
import com.mraof.minestuck.player.Title;
import com.mraof.minestuck.skaianet.TitleSelectionHook;
import com.mraof.minestuck.world.storage.MSExtraData;
import net.minecraft.server.MinecraftServer;
Expand Down Expand Up @@ -218,13 +222,9 @@ public static void playerChangedDimension(PlayerEvent.PlayerChangedDimensionEven
@SubscribeEvent(priority=EventPriority.LOW, receiveCanceled=false)
public static void onServerChat(ServerChatEvent event)
{
ServerPlayer player = event.getPlayer();
if(!(player instanceof FakePlayer))
{
Modus modus = PlayerSavedData.getData(player).getModus();
if(modus instanceof HashMapModus)
((HashMapModus) modus).onChatMessage(event.getPlayer(), event.getMessage().getString());
}
Modus modus = CaptchaDeckHandler.getModus(event.getPlayer());
if(modus instanceof HashMapModus hashMapModus)
hashMapModus.onChatMessage(event.getPlayer(), event.getMessage().getString());
}

@SubscribeEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.mraof.minestuck.player.PlayerBoondollars;
import com.mraof.minestuck.player.PlayerData;
import com.mraof.minestuck.player.PlayerSavedData;
import com.mraof.minestuck.util.MSCapabilities;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -27,13 +28,17 @@
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.LogicalSide;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.common.util.INBTSerializable;
import net.neoforged.neoforge.event.entity.living.LivingDropsEvent;
import net.neoforged.neoforge.network.PacketDistributor;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Objects;

//todo this class could use some spring cleaning
@Mod.EventBusSubscriber(modid = Minestuck.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public final class CaptchaDeckHandler
{
Expand All @@ -43,14 +48,27 @@ public final class CaptchaDeckHandler
public static final int EMPTY_CARD = -2;

@SubscribeEvent(priority = EventPriority.LOWEST)
public static void onPlayerDrops(LivingDropsEvent event)
private static void onPlayerDrops(LivingDropsEvent event)
{
if(event.getEntity() instanceof ServerPlayer player && !event.getEntity().level().getGameRules().getBoolean(GameRules.RULE_KEEPINVENTORY))
{
dropSylladex(player);
}
}

@SubscribeEvent
private static void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event)
{
ServerPlayer player = (ServerPlayer) event.getEntity();

ModusHolder modusHolder = getHolder(player);
if(modusHolder.modus != null)
player.connection.send(ModusDataPacket.create(modusHolder.modus));

if(modusHolder.modus == null && !modusHolder.givenModus)
CaptchaDeckHandler.tryGiveStartingModus(modusHolder, player);
}

public static Modus createClientModus(ResourceLocation name)
{
ModusType<?> type = ModusTypes.REGISTRY.get(name);
Expand Down Expand Up @@ -112,8 +130,10 @@ private static ItemStack changeModus(ServerPlayer player, ItemStack modusItem, @

if(oldModus == null)
{
PlayerData data = PlayerSavedData.getData(player);
newModus.initModus(modusItem, player, null, data.hasGivenModus() ? 0 : MinestuckConfig.SERVER.initialModusSize.get());
ModusHolder modusHolder = getHolder(player);
newModus.initModus(modusItem, player, null, modusHolder.givenModus
? 0
: MinestuckConfig.SERVER.initialModusSize.get());
}
else
{
Expand All @@ -131,7 +151,7 @@ private static ItemStack changeModus(ServerPlayer player, ItemStack modusItem, @
}
}

PlayerSavedData.getData(player).setModus(newModus);
setModus(getHolder(player), player, newModus);

MSCriteriaTriggers.CHANGE_MODUS.get().trigger(player, newModus);

Expand Down Expand Up @@ -322,11 +342,11 @@ private static void dropSylladex(ServerPlayer player)
if(MinestuckConfig.SERVER.sylladexDropMode.get() == MinestuckConfig.DropMode.ALL)
{
player.drop(modus.getModusItem(), true, false);
PlayerSavedData.getData(player).setModus(null);
setModus(getHolder(player), player, null);
} else
{
modus.initModus(null, player, null, size);
PacketDistributor.PLAYER.with(player).send(ModusDataPacket.create(modus));
player.connection.send(ModusDataPacket.create(modus));
}
}

Expand Down Expand Up @@ -372,9 +392,13 @@ private static boolean hasModus(ServerPlayer player)
return getModus(player) != null;
}

@Nullable
public static Modus getModus(ServerPlayer player)
{
return PlayerSavedData.getData(player).getModus();
PlayerData playerData = PlayerSavedData.getData(player);
if(playerData == null)
return null;
return playerData.getData(MSCapabilities.MODUS_HOLDER).modus;
}

private static boolean canMergeItemStacks(ItemStack stack1, ItemStack stack2)
Expand All @@ -387,4 +411,79 @@ private static boolean canPlayerUseModus(ServerPlayer player)
{
return !player.isSpectator() && ServerEditHandler.getData(player) == null;
}
}

private static void setModus(ModusHolder modusHolder, ServerPlayer player, @Nullable Modus modus)
{
if(modusHolder.modus == modus)
return;

modusHolder.modus = modus;
if(modus != null)
modusHolder.givenModus = true;
player.connection.send(ModusDataPacket.create(modus));
}

private static void tryGiveStartingModus(ModusHolder modusHolder, ServerPlayer player)
{
List<ModusType<?>> startingTypes = StartingModusManager.getStartingModusTypes();

if(startingTypes.isEmpty())
return;

ModusType<?> type = startingTypes.get(player.level().random.nextInt(startingTypes.size()));
if(type == null)
{
modusHolder.givenModus = true;
return;
}

Modus modus = type.createServerSide();
if(modus == null)
{
LOGGER.warn("Couldn't create a starting modus type {}.", ModusTypes.REGISTRY.getKey(type));
return;
}

modus.initModus(null, player, null, MinestuckConfig.SERVER.initialModusSize.get());
setModus(modusHolder, player, modus);
}

private static ModusHolder getHolder(ServerPlayer player)
{
PlayerData data = Objects.requireNonNull(PlayerSavedData.getData(player));
return data.getData(MSCapabilities.MODUS_HOLDER);
}

public static class ModusHolder implements INBTSerializable<CompoundTag>
{
private boolean givenModus = false;
@Nullable
private Modus modus = null;

@Override
public CompoundTag serializeNBT()
{
CompoundTag nbt = new CompoundTag();

CompoundTag modusTag = writeToNBT(modus);
if(modusTag != null)
nbt.put("modus", modusTag);
else
nbt.putBoolean("given_modus", givenModus);

return null;
}

@Override
public void deserializeNBT(CompoundTag nbt)
{
if (nbt.contains("modus"))
{
this.modus = readFromNBT(nbt.getCompound("modus"), LogicalSide.SERVER);
givenModus = true;
}
else
givenModus = nbt.getBoolean("given_modus");
}
}
}
86 changes: 1 addition & 85 deletions src/main/java/com/mraof/minestuck/player/PlayerData.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.mraof.minestuck.player;

import com.mraof.minestuck.Minestuck;
import com.mraof.minestuck.MinestuckConfig;
import com.mraof.minestuck.inventory.captchalogue.*;
import com.mraof.minestuck.network.data.ModusDataPacket;
import com.mraof.minestuck.world.MSDimensions;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
Expand All @@ -15,19 +12,14 @@
import net.minecraft.util.Mth;
import net.minecraft.world.level.Level;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.LogicalSide;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.attachment.AttachmentHolder;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
import net.neoforged.neoforge.network.PacketDistributor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -39,13 +31,10 @@
@Mod.EventBusSubscriber(modid = Minestuck.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public final class PlayerData extends AttachmentHolder
{
private static final Logger LOGGER = LogManager.getLogger();

@SubscribeEvent
public static void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event)
{
ServerPlayer player = (ServerPlayer) event.getEntity();
PlayerSavedData.getData(player).onPlayerLoggedIn(player);
MSDimensions.sendDimensionData(player);
}

Expand All @@ -54,9 +43,6 @@ public static void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event)

private final MinecraftServer mcServer;

private boolean givenModus;
private Modus modus;

private final Map<ResourceLocation, Integer> consortReputation = new HashMap<>();

PlayerData(MinecraftServer mcServer, PlayerIdentifier player)
Expand All @@ -73,13 +59,6 @@ public static void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event)
if(nbt.contains(ATTACHMENTS_NBT_KEY, Tag.TAG_COMPOUND))
this.deserializeAttachments(nbt.getCompound(ATTACHMENTS_NBT_KEY));

if (nbt.contains("modus"))
{
this.modus = CaptchaDeckHandler.readFromNBT(nbt.getCompound("modus"), LogicalSide.SERVER);
givenModus = true;
}
else givenModus = nbt.getBoolean("given_modus");

ListTag list = nbt.getList("consort_reputation", Tag.TAG_COMPOUND);
for(int i = 0; i < list.size(); i++)
{
Expand All @@ -99,10 +78,6 @@ CompoundTag writeToNBT()
if(attachments != null)
nbt.put(ATTACHMENTS_NBT_KEY, attachments);

if (this.modus != null)
nbt.put("modus", CaptchaDeckHandler.writeToNBT(modus));
else nbt.putBoolean("given_modus", givenModus);

ListTag list = new ListTag();
for(Map.Entry<ResourceLocation, Integer> entry : consortReputation.entrySet())
{
Expand All @@ -116,34 +91,6 @@ CompoundTag writeToNBT()
return nbt;
}

public Modus getModus()
{
return modus;
}

public void setModus(@Nullable Modus modus)
{
if(this.modus != modus)
{
this.modus = modus;
if(modus != null)
setGivenModus();
ServerPlayer player = this.getPlayer();
if(player != null)
PacketDistributor.PLAYER.with(player).send(ModusDataPacket.create(modus));
}
}

public boolean hasGivenModus()
{
return givenModus;
}

private void setGivenModus()
{
givenModus = true;
}

public int getConsortReputation(ResourceKey<Level> dim)
{
return consortReputation.getOrDefault(dim.location(), 0);
Expand All @@ -158,39 +105,8 @@ public void addConsortReputation(int amount, ResourceKey<Level> dim)
consortReputation.put(dim.location(), newRep);
}

private void tryGiveStartingModus(ServerPlayer player)
{
List<ModusType<?>> startingTypes = StartingModusManager.getStartingModusTypes();

if(startingTypes.isEmpty())
return;

ModusType<?> type = startingTypes.get(player.level().random.nextInt(startingTypes.size()));
if(type == null)
{
setGivenModus();
return;
}

Modus modus = type.createServerSide();
if(modus != null)
{
modus.initModus(null, player, null, MinestuckConfig.SERVER.initialModusSize.get());
setModus(modus);
} else LOGGER.warn("Couldn't create a starting modus type {}.", ModusTypes.REGISTRY.getKey(type));
}

public void onPlayerLoggedIn(ServerPlayer player)
{
if(getModus() != null)
PacketDistributor.PLAYER.with(player).send(ModusDataPacket.create(getModus()));

if(getModus() == null && !hasGivenModus())
tryGiveStartingModus(player);
}

@Nullable
ServerPlayer getPlayer()
public ServerPlayer getPlayer()
{
return identifier.getPlayer(mcServer);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/mraof/minestuck/util/MSCapabilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.mraof.minestuck.computer.editmode.EditmodeLocations;
import com.mraof.minestuck.entity.dialogue.DialogueComponent;
import com.mraof.minestuck.fluid.MSFluidType;
import com.mraof.minestuck.inventory.captchalogue.CaptchaDeckHandler;
import com.mraof.minestuck.inventory.musicplayer.MusicPlaying;
import com.mraof.minestuck.player.Echeladder;
import com.mraof.minestuck.player.GristCache;
Expand Down Expand Up @@ -35,6 +36,8 @@ public final class MSCapabilities
{
public static final DeferredRegister<AttachmentType<?>> ATTACHMENT_REGISTER = DeferredRegister.create(NeoForgeRegistries.Keys.ATTACHMENT_TYPES, Minestuck.MOD_ID);

public static final Supplier<AttachmentType<CaptchaDeckHandler.ModusHolder>> MODUS_HOLDER = ATTACHMENT_REGISTER.register("modus_holder",
() -> AttachmentType.serializable(restricted(CaptchaDeckHandler.ModusHolder::new, PlayerData.class)).build());
public static final Supplier<AttachmentType<Integer>> PLAYER_COLOR = ATTACHMENT_REGISTER.register("player_color",
() -> AttachmentType.builder(restricted(() -> ColorHandler.BuiltinColors.DEFAULT_COLOR, PlayerData.class)).serialize(Codec.INT).build());
public static final Supplier<AttachmentType<Long>> BOONDOLLARS = ATTACHMENT_REGISTER.register("boondollars",
Expand Down

0 comments on commit 5fb11fc

Please sign in to comment.