Skip to content

Commit

Permalink
Remove APPLY_TO_PLAYERS event
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Oct 9, 2023
1 parent 2dc7c6d commit 350e891
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.lovetropics.minigames.common.core.game.behavior.action;

import com.lovetropics.minigames.common.core.game.IGamePhase;
import com.lovetropics.minigames.common.core.game.behavior.event.EventRegistrar;
import com.lovetropics.minigames.common.core.game.behavior.event.GameEventListeners;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import net.minecraft.util.ExtraCodecs;
import org.apache.commons.lang3.function.ToBooleanBiFunction;

import java.util.List;
import java.util.function.Function;
Expand All @@ -19,5 +21,7 @@ public interface ActionTarget<T> {

boolean apply(IGamePhase game, GameEventListeners listeners, GameActionContext context, Iterable<T> sources);

void listenAndCaptureSource(EventRegistrar listeners, ToBooleanBiFunction<GameActionContext, Iterable<T>> listener);

Codec<? extends ActionTarget<T>> type();
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ public class GameActionList<T, A extends ActionTarget<T>> {
.xmap(either -> either.map(Function.identity(), Function.identity()), Either::right);
public static final Codec<GameActionList<?, ?>> TYPE_SAFE_CODEC = (Codec<GameActionList<?,?>>) (Object) CODEC;

public static <T, A extends ActionTarget<T>> Codec<GameActionList<T, A>> mandateType(Supplier<Codec<A>> type) {
final Function<GameActionList<?, ?>, DataResult<GameActionList<T, A>>> function = gameActionList -> {
if (gameActionList.target.type() != type.get()) {
return DataResult.error(() -> "Action list target must be of type: " + ActionTargetTypes.REGISTRY.get().getKey(type.get()));
}
return DataResult.success((GameActionList<T, A>) gameActionList);
};
return TYPE_SAFE_CODEC.flatXmap(function, function);
}

private final List<IGameBehavior> behaviors;
public final A target;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.lovetropics.minigames.common.core.game.behavior.action;

import com.lovetropics.minigames.common.core.game.IGamePhase;
import com.lovetropics.minigames.common.core.game.behavior.event.EventRegistrar;
import com.lovetropics.minigames.common.core.game.behavior.event.GameActionEvents;
import com.lovetropics.minigames.common.core.game.behavior.event.GameEventListeners;
import com.mojang.serialization.Codec;
import org.apache.commons.lang3.function.ToBooleanBiFunction;

import java.util.List;

public record NoneActionTarget() implements ActionTarget<Void> {
public static final Codec<NoneActionTarget> CODEC = Codec.unit(NoneActionTarget::new);
public static final NoneActionTarget INSTANCE = new NoneActionTarget();
public static final Codec<NoneActionTarget> CODEC = Codec.unit(INSTANCE);

@Override
public List<Void> resolve(IGamePhase phase, Iterable<Void> sources) {
Expand All @@ -19,6 +23,11 @@ public boolean apply(IGamePhase game, GameEventListeners listeners, GameActionCo
return false;
}

@Override
public void listenAndCaptureSource(EventRegistrar listeners, ToBooleanBiFunction<GameActionContext, Iterable<Void>> listener) {
listeners.listen(GameActionEvents.APPLY, context -> listener.applyAsBoolean(context, List.of()));
}

@Override
public Codec<? extends ActionTarget<Void>> type() {
return ActionTargetTypes.NONE.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.google.common.collect.Lists;
import com.lovetropics.lib.codec.MoreCodecs;
import com.lovetropics.minigames.common.core.game.IGamePhase;
import com.lovetropics.minigames.common.core.game.behavior.event.EventRegistrar;
import com.lovetropics.minigames.common.core.game.behavior.event.GameActionEvents;
import com.lovetropics.minigames.common.core.game.behavior.event.GameEventListeners;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.StringRepresentable;
import org.apache.commons.lang3.function.ToBooleanBiFunction;

import java.util.List;

Expand All @@ -25,13 +27,18 @@ public List<ServerPlayer> resolve(IGamePhase phase, Iterable<ServerPlayer> sourc

@Override
public boolean apply(IGamePhase game, GameEventListeners listeners, GameActionContext actionContext, Iterable<ServerPlayer> sources) {
boolean result = listeners.invoker(GameActionEvents.APPLY_TO_PLAYERS).apply(actionContext, sources);
boolean result = false;
for (ServerPlayer target : target.resolve(game, sources)) {
result |= listeners.invoker(GameActionEvents.APPLY_TO_PLAYER).apply(actionContext, target);
}
return result;
}

@Override
public void listenAndCaptureSource(EventRegistrar listeners, ToBooleanBiFunction<GameActionContext, Iterable<ServerPlayer>> listener) {
listeners.listen(GameActionEvents.APPLY_TO_PLAYER, (context, target1) -> listener.applyAsBoolean(context, List.of(target1)));
}

@Override
public Codec<? extends ActionTarget<ServerPlayer>> type() {
return ActionTargetTypes.PLAYER.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import com.lovetropics.minigames.common.content.biodiversity_blitz.plot.Plot;
import com.lovetropics.minigames.common.content.biodiversity_blitz.plot.PlotsState;
import com.lovetropics.minigames.common.core.game.IGamePhase;
import com.lovetropics.minigames.common.core.game.behavior.event.EventRegistrar;
import com.lovetropics.minigames.common.core.game.behavior.event.GameActionEvents;
import com.lovetropics.minigames.common.core.game.behavior.event.GameEventListeners;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.StringRepresentable;
import org.apache.commons.lang3.function.ToBooleanBiFunction;

import java.util.List;

Expand All @@ -33,6 +34,11 @@ public boolean apply(IGamePhase game, GameEventListeners listeners, GameActionCo
return result;
}

@Override
public void listenAndCaptureSource(EventRegistrar listeners, ToBooleanBiFunction<GameActionContext, Iterable<Plot>> listener) {
listeners.listen(GameActionEvents.APPLY_TO_PLOT, (context, plot) -> listener.applyAsBoolean(context, List.of(plot)));
}

@Override
public Codec<? extends ActionTarget<Plot>> type() {
return ActionTargetTypes.PLOT.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ public final class GameActionEvents {
return applied;
});

public static final GameEventType<ApplyToPlayers> APPLY_TO_PLAYERS = GameEventType.create(ApplyToPlayers.class, listeners -> (context, sources) -> {
boolean applied = false;
for (ApplyToPlayers listener : listeners) {
applied |= listener.apply(context, sources);
}
return applied;
});

public static final GameEventType<ApplyToPlayer> APPLY_TO_PLAYER = GameEventType.create(ApplyToPlayer.class, listeners -> (context, target) -> {
boolean applied = false;
for (ApplyToPlayer listener : listeners) {
Expand All @@ -41,7 +33,7 @@ private GameActionEvents() {
}

public static boolean matches(GameEventType<?> type) {
return type == APPLY || type == APPLY_TO_PLAYERS || type == APPLY_TO_PLOT || type == APPLY_TO_PLAYER;
return type == APPLY || type == APPLY_TO_PLOT || type == APPLY_TO_PLAYER;
}

public interface Apply {
Expand All @@ -52,10 +44,6 @@ public interface ApplyToPlayer {
boolean apply(GameActionContext context, ServerPlayer target);
}

public interface ApplyToPlayers {
boolean apply(GameActionContext context, Iterable<ServerPlayer> sources);
}

public interface ApplyToPlot {
boolean apply(GameActionContext context, Plot plot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

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.action.ActionTarget;
import com.lovetropics.minigames.common.core.game.behavior.action.GameActionContext;
import com.lovetropics.minigames.common.core.game.behavior.action.GameActionList;
import com.lovetropics.minigames.common.core.game.behavior.action.NoneActionTarget;
import com.lovetropics.minigames.common.core.game.behavior.event.EventRegistrar;
import com.lovetropics.minigames.common.core.game.behavior.event.GameActionEvents;
import com.lovetropics.minigames.common.core.game.behavior.event.GamePhaseEvents;
Expand All @@ -18,35 +20,36 @@
import net.minecraft.sounds.SoundSource;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public final class CountdownAction implements IGameBehavior {
public static final Codec<CountdownAction> CODEC = RecordCodecBuilder.create(i -> i.group(
Codec.LONG.fieldOf("countdown").forGetter(c -> c.countdown / 20),
TemplatedText.CODEC.fieldOf("warning").forGetter(c -> c.warning),
GameActionList.MAP_CODEC.forGetter(c -> c.actions)
GameActionList.MAP_CODEC.forGetter(c -> c.actions),
ActionTarget.CODEC.optionalFieldOf("target", NoneActionTarget.INSTANCE).forGetter(c -> c.target)
).apply(i, CountdownAction::new));

private final long countdown;
private final TemplatedText warning;
private final GameActionList actions;
private final ActionTarget<?> target;

private final LinkedList<QueueEntry> queue = new LinkedList<>();

public CountdownAction(long countdown, TemplatedText warning, GameActionList actions) {
public CountdownAction(long countdown, TemplatedText warning, GameActionList actions, ActionTarget<?> target) {
this.countdown = countdown * 20;
this.warning = warning;
this.actions = actions;
this.target = target;
}

@Override
public void register(IGamePhase game, EventRegistrar events) {
actions.register(game, events);

events.listen(GameActionEvents.APPLY_TO_PLAYERS, (context, sources) -> {
queue.add(new QueueEntry(game.ticks() + countdown, context, sources));
return true;
});
target.listenAndCaptureSource(events, (context, objects) -> queue.add(new QueueEntry(game.ticks() + countdown, context, (Iterable)objects)));

events.listen(GamePhaseEvents.TICK, () -> {
if (!queue.isEmpty()) {
Expand All @@ -58,7 +61,11 @@ public void register(IGamePhase game, EventRegistrar events) {
private boolean tickQueuedAction(IGamePhase game, QueueEntry entry) {
long remainingTicks = entry.time() - game.ticks();
if (remainingTicks <= 0) {
return actions.applyPlayer(game, entry.context(), entry.sources());
if (actions.target.type() == target.type()) {
return actions.applyIf(target::type, game, entry.context, entry.sources);
}

return actions.apply(game, entry.context);
} else {
for (ServerPlayer player : game.getAllPlayers()) {
this.tickCountdown(player, remainingTicks);
Expand All @@ -76,6 +83,6 @@ private void tickCountdown(ServerPlayer player, long remainingTicks) {
}
}

private record QueueEntry(long time, GameActionContext context, Iterable<ServerPlayer> sources) {
private record QueueEntry(long time, GameActionContext context, Iterable<Object> sources) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import com.lovetropics.lib.codec.MoreCodecs;
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.action.ActionTargetTypes;
import com.lovetropics.minigames.common.core.game.behavior.action.GameActionContext;
import com.lovetropics.minigames.common.core.game.behavior.action.GameActionParameter;
import com.lovetropics.minigames.common.core.game.behavior.action.GameActionList;
import com.lovetropics.minigames.common.core.game.behavior.action.PlayerActionTarget;
import com.lovetropics.minigames.common.core.game.behavior.event.EventRegistrar;
import com.lovetropics.minigames.common.core.game.behavior.event.GamePackageEvents;
import com.lovetropics.minigames.common.core.game.state.GamePackageState;
Expand All @@ -23,12 +25,12 @@
public final class DonationPackageBehavior implements IGameBehavior {
public static final Codec<DonationPackageBehavior> CODEC = RecordCodecBuilder.create(i -> i.group(
DonationPackageData.CODEC.forGetter(c -> c.data),
MoreCodecs.strictOptionalFieldOf(GameActionList.CODEC, "receive_actions", GameActionList.EMPTY).forGetter(c -> c.receiveActions)
MoreCodecs.strictOptionalFieldOf(GameActionList.mandateType(ActionTargetTypes.PLAYER), "receive_actions", GameActionList.EMPTY).forGetter(c -> c.receiveActions)
).apply(i, DonationPackageBehavior::new));

private static final Logger LOGGER = LogManager.getLogger(DonationPackageBehavior.class);

public DonationPackageBehavior(DonationPackageData data, GameActionList receiveActions) {
public DonationPackageBehavior(DonationPackageData data, GameActionList<ServerPlayer, PlayerActionTarget> receiveActions) {
this.data = data;
this.receiveActions = receiveActions;
}
Expand All @@ -46,7 +48,7 @@ public enum PlayerSelect {
}

private final DonationPackageData data;
private final GameActionList receiveActions;
private final GameActionList<ServerPlayer, PlayerActionTarget> receiveActions;

@Override
public void register(IGamePhase game, EventRegistrar events) {
Expand Down

0 comments on commit 350e891

Please sign in to comment.