Skip to content

Commit

Permalink
Add behaviour to glow teammates
Browse files Browse the repository at this point in the history
Fixes #128
  • Loading branch information
Matyrobbrt committed Oct 1, 2023
1 parent b4d52d1 commit 721eefc
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import com.lovetropics.minigames.common.core.game.behavior.instances.statistics.PlaceByStatisticBehavior;
import com.lovetropics.minigames.common.core.game.behavior.instances.statistics.TimeSurvivedTrackerBehavior;
import com.lovetropics.minigames.common.core.game.behavior.instances.team.SetupTeamsBehavior;
import com.lovetropics.minigames.common.core.game.behavior.instances.team.SyncTeamsBehavior;
import com.lovetropics.minigames.common.core.game.behavior.instances.team.TeamWinTrigger;
import com.lovetropics.minigames.common.core.game.behavior.instances.team.TeamsBehavior;
import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.BindControlsBehavior;
Expand Down Expand Up @@ -150,6 +151,7 @@ public class GameBehaviorTypes {
public static final GameBehaviorEntry<SetMaxHealthBehavior> SET_MAX_HEALTH = register("set_max_health", SetMaxHealthBehavior.CODEC);
public static final GameBehaviorEntry<IndividualWinTrigger> INDIVIDUAL_WIN_TRIGGER = register("individual_win_trigger", IndividualWinTrigger.CODEC);
public static final GameBehaviorEntry<TeamWinTrigger> TEAM_WIN_TRIGGER = register("team_win_trigger", TeamWinTrigger.CODEC);
public static final GameBehaviorEntry<SyncTeamsBehavior> SYNC_TEAMS = register("sync_teams", SyncTeamsBehavior.CODEC);
public static final GameBehaviorEntry<EquipParticipantsBehavior> EQUIP_PARTICIPANTS = register("equip_participants", EquipParticipantsBehavior.CODEC);
public static final GameBehaviorEntry<ArmorParticipantsBehavior> ARMOR_PARTICIPANTS = register("armor_participants", ArmorParticipantsBehavior.CODEC);
public static final GameBehaviorEntry<SetTimeSpeedBehavior> SET_TIME_SPEED = register("set_time_speed", SetTimeSpeedBehavior.CODEC);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,20 @@ public final class GameTeamEvents {
}
});

public static final GameEventType<RemoveFromTeam> REMOVE_FROM_TEAM = GameEventType.create(RemoveFromTeam.class, listeners -> (player, teams, team) -> {
for (RemoveFromTeam listener : listeners) {
listener.onRemoveFromTeam(player, teams, team);
}
});

private GameTeamEvents() {
}

public interface SetGameTeam {
void onSetGameTeam(ServerPlayer player, TeamState teams, GameTeamKey team);
}

public interface RemoveFromTeam {
void onRemoveFromTeam(ServerPlayer player, TeamState teams, GameTeamKey team);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.lovetropics.minigames.common.core.game.behavior.instances.team;

import com.lovetropics.minigames.common.core.game.GameException;
import com.lovetropics.minigames.common.core.game.IGamePhase;
import com.lovetropics.minigames.common.core.game.behavior.IGameBehavior;
import com.lovetropics.minigames.common.core.game.behavior.event.EventRegistrar;
import com.lovetropics.minigames.common.core.game.behavior.event.GamePhaseEvents;
import com.lovetropics.minigames.common.core.game.behavior.event.GameTeamEvents;
import com.lovetropics.minigames.common.core.game.client_state.GameClientStateSender;
import com.lovetropics.minigames.common.core.game.client_state.GameClientStateTypes;
import com.lovetropics.minigames.common.core.game.client_state.instance.TeamMembersClientState;
import com.lovetropics.minigames.common.core.game.state.team.GameTeamKey;
import com.lovetropics.minigames.common.core.game.state.team.TeamState;
import com.lovetropics.minigames.common.core.network.LoveTropicsNetwork;
import com.lovetropics.minigames.common.core.network.SetGameClientStateMessage;
import com.mojang.serialization.Codec;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.PacketDistributor;

public class SyncTeamsBehavior implements IGameBehavior {
public static final Codec<SyncTeamsBehavior> CODEC = Codec.unit(SyncTeamsBehavior::new);

@Override
public void register(IGamePhase game, EventRegistrar events) throws GameException {
events.listen(GameTeamEvents.SET_GAME_TEAM, (player, teams, team) -> sendSync(teams, team));
events.listen(GameTeamEvents.REMOVE_FROM_TEAM, (player, teams, team) -> {
sendSync(teams, team);
GameClientStateSender.get().byPlayer(player).enqueueRemove(GameClientStateTypes.TEAM_MEMBERS.get());
});

events.listen(GamePhaseEvents.STOP, reason -> LoveTropicsNetwork.CHANNEL.send(PacketDistributor.ALL.with(() -> null), SetGameClientStateMessage.remove(GameClientStateTypes.TEAM_MEMBERS.get())));
}

private void sendSync(TeamState teams, GameTeamKey key) {
final var team = teams.getPlayersForTeam(key);
team.forEach(player -> GameClientStateSender.get().byPlayer(player).enqueueSet(new TeamMembersClientState(team.stream().filter(p -> p != player)
.map(ServerPlayer::getUUID).toList())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public void register(IGamePhase game, EventRegistrar events) {
events.listen(GamePhaseEvents.DESTROY, () -> onDestroy(game));
events.listen(GamePlayerEvents.ALLOCATE_ROLES, allocator -> reassignPlayerRoles(game, allocator));

events.listen(GamePlayerEvents.SET_ROLE, this::onPlayerSetRole);
events.listen(GamePlayerEvents.LEAVE, this::removePlayerFromTeams);
events.listen(GamePlayerEvents.SET_ROLE, (player, role, lastRole) -> onPlayerSetRole(game, player, role, lastRole));
events.listen(GamePlayerEvents.LEAVE, player -> removePlayerFromTeams(game, player));
events.listen(GamePlayerEvents.DAMAGE, this::onPlayerHurt);
events.listen(GamePlayerEvents.ATTACK, this::onPlayerAttack);

Expand Down Expand Up @@ -135,9 +135,9 @@ private void onDestroy(IGamePhase game) {
}
}

private void onPlayerSetRole(ServerPlayer player, @Nullable PlayerRole role, @Nullable PlayerRole lastRole) {
private void onPlayerSetRole(IGamePhase game, ServerPlayer player, @Nullable PlayerRole role, @Nullable PlayerRole lastRole) {
if (lastRole == PlayerRole.PARTICIPANT && role != PlayerRole.PARTICIPANT) {
removePlayerFromTeams(player);
removePlayerFromTeams(game, player);
}
}

Expand Down Expand Up @@ -166,8 +166,11 @@ private void addPlayerToTeam(IGamePhase game, ServerPlayer player, GameTeamKey t
);
}

private void removePlayerFromTeams(ServerPlayer player) {
teams.removePlayer(player);
private void removePlayerFromTeams(IGamePhase game, ServerPlayer player) {
final var teamKey = teams.removePlayer(player);
if (teamKey != null) {
game.invoker(GameTeamEvents.REMOVE_FROM_TEAM).onRemoveFromTeam(player, teams, teamKey);
}

ServerScoreboard scoreboard = player.server.getScoreboard();
scoreboard.removePlayerFromTeam(player.getScoreboardName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import com.lovetropics.minigames.LoveTropics;
import com.lovetropics.minigames.common.core.game.client_state.instance.BeaconClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.FogClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.GlowTeamMembersState;
import com.lovetropics.minigames.common.core.game.client_state.instance.HealthTagClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.ReplaceTexturesClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.ResourcePackClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.SidebarClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.SpectatingClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.TeamMembersClientState;
import com.lovetropics.minigames.common.core.game.client_state.instance.TimeInterpolationClientState;
import com.lovetropics.minigames.common.util.registry.GameClientTweakEntry;
import com.lovetropics.minigames.common.util.registry.LoveTropicsRegistrate;
Expand Down Expand Up @@ -43,6 +45,8 @@ public final class GameClientStateTypes {
public static final GameClientTweakEntry<SidebarClientState> SIDEBAR = register("sidebar", SidebarClientState.CODEC);
public static final GameClientTweakEntry<BeaconClientState> BEACON = register("beacon", BeaconClientState.CODEC);
public static final GameClientTweakEntry<FogClientState> FOG = register("fog", FogClientState.CODEC);
public static final GameClientTweakEntry<TeamMembersClientState> TEAM_MEMBERS = register("team_members", TeamMembersClientState.CODEC);
public static final GameClientTweakEntry<GlowTeamMembersState> GLOW_TEAM_MEMBERS = register("glow_team_members", Codec.unit(GlowTeamMembersState.INSTANCE));

public static <T extends GameClientState> GameClientTweakEntry<T> register(final String name, final Codec<T> codec) {
return REGISTRATE.object(name)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.lovetropics.minigames.common.core.game.client_state.instance;

import com.lovetropics.minigames.common.core.game.client_state.GameClientState;
import com.lovetropics.minigames.common.core.game.client_state.GameClientStateType;
import com.lovetropics.minigames.common.core.game.client_state.GameClientStateTypes;

public class GlowTeamMembersState implements GameClientState {
public static final GlowTeamMembersState INSTANCE = new GlowTeamMembersState();
private GlowTeamMembersState() {}

@Override
public GameClientStateType<?> getType() {
return GameClientStateTypes.GLOW_TEAM_MEMBERS.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.lovetropics.minigames.common.core.game.client_state.instance;

import com.lovetropics.minigames.common.core.game.client_state.GameClientState;
import com.lovetropics.minigames.common.core.game.client_state.GameClientStateType;
import com.lovetropics.minigames.common.core.game.client_state.GameClientStateTypes;
import com.mojang.serialization.Codec;
import net.minecraft.core.UUIDUtil;

import java.util.List;
import java.util.UUID;

public record TeamMembersClientState(List<UUID> teamMembers) implements GameClientState {
public static final Codec<TeamMembersClientState> CODEC = UUIDUtil.CODEC.listOf().fieldOf("members")
.xmap(TeamMembersClientState::new, TeamMembersClientState::teamMembers)
.codec();

@Override
public GameClientStateType<?> getType() {
return GameClientStateTypes.TEAM_MEMBERS.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.lovetropics.minigames.mixin.client;

import com.lovetropics.minigames.client.game.ClientGameStateManager;
import com.lovetropics.minigames.common.core.game.client_state.GameClientStateTypes;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import javax.annotation.Nullable;

@Mixin(Minecraft.class)
public class MinecraftMixin {
@Nullable
@Shadow
private LocalPlayer player;

@Inject(at = @At("HEAD"), method = "shouldEntityAppearGlowing", cancellable = true)
private void ltminigames$glowingTeamMembers(Entity entity, CallbackInfoReturnable<Boolean> cir) {
if (entity.getType() == EntityType.PLAYER && ClientGameStateManager.getOrNull(GameClientStateTypes.GLOW_TEAM_MEMBERS) != null) {
final var team = ClientGameStateManager.getOrNull(GameClientStateTypes.TEAM_MEMBERS);

if (team != null && (team.teamMembers().contains(entity.getUUID()) || player == entity)) {
cir.setReturnValue(true);
}
}
}
}
3 changes: 2 additions & 1 deletion src/main/resources/ltminigames.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"client.BossOverlayGuiMixin",
"client.HotbarOverride",
"client.PlayerTabOverlayGuiMixin",
"client.PoseStackAccessor"
"client.PoseStackAccessor",
"client.MinecraftMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 721eefc

Please sign in to comment.