Skip to content

Commit

Permalink
Add module enabling/disabling events
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugman76 committed Dec 6, 2024
1 parent 3b470bf commit e2b29d3
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 118 deletions.
14 changes: 13 additions & 1 deletion src/main/java/com/hugman/uhc/command/UHCCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.hugman.uhc.config.UHCGameConfig;
import com.hugman.uhc.game.UHCAttachments;
import com.hugman.uhc.module.Module;
import com.hugman.uhc.module.ModuleEvents;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
Expand All @@ -15,6 +16,8 @@
import net.minecraft.text.Text;
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.api.game.GameSpaceManager;
import xyz.nucleoid.stimuli.EventInvokers;
import xyz.nucleoid.stimuli.Stimuli;

import java.util.Objects;

Expand Down Expand Up @@ -77,12 +80,17 @@ private static int displayModules(CommandContext<ServerCommandSource> context) t

private static int enableModule(CommandContext<ServerCommandSource> context, RegistryEntry<Module> module) throws CommandSyntaxException {
ServerCommandSource source = context.getSource();
var manager = Objects.requireNonNull(GameSpaceManager.get().byWorld(source.getWorld())).getAttachment(UHCAttachments.MODULE_MANAGER);
var space = Objects.requireNonNull(GameSpaceManager.get().byWorld(source.getWorld()));
var manager = space.getAttachment(UHCAttachments.MODULE_MANAGER);
if (manager == null) {
throw NO_MANAGER_ACTIVATED.create();
}

if (manager.enableModule(module)) {
try (EventInvokers invokers = Stimuli.select().forCommandSource(context.getSource())) {
(invokers.get(ModuleEvents.ENABLE)).onEnable(module);
}

source.sendFeedback(() -> Text.translatable("command.uhc.modules.enable.success", module.value().name()), true); //TODO: translate this
return Command.SINGLE_SUCCESS;
} else {
Expand All @@ -98,6 +106,10 @@ private static int disableModule(CommandContext<ServerCommandSource> context, Re
}

if (manager.disableModule(module)) {
try (EventInvokers invokers = Stimuli.select().forCommandSource(context.getSource())) {
(invokers.get(ModuleEvents.DISABLE)).onDisable(module);
}

source.sendFeedback(() -> Text.translatable("command.uhc.modules.disable.success", module.value().name()), true); //TODO: translate this
return Command.SINGLE_SUCCESS;
} else {
Expand Down
76 changes: 74 additions & 2 deletions src/main/java/com/hugman/uhc/game/ModuleManager.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
package com.hugman.uhc.game;

import com.hugman.uhc.modifier.Modifier;
import com.hugman.uhc.modifier.ModifierType;
import com.hugman.uhc.modifier.*;
import com.hugman.uhc.module.Module;
import com.hugman.uhc.module.ModuleEvents;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.SimpleGui;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.entry.RegistryEntryList;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.*;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.explosion.Explosion;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.plasmid.api.game.GameActivity;
import xyz.nucleoid.plasmid.api.game.event.GamePlayerEvents;
import xyz.nucleoid.stimuli.EventInvokers;
import xyz.nucleoid.stimuli.Stimuli;
import xyz.nucleoid.stimuli.event.DroppedItemsResult;
import xyz.nucleoid.stimuli.event.EventResult;
import xyz.nucleoid.stimuli.event.block.BlockBreakEvent;
import xyz.nucleoid.stimuli.event.block.BlockDropItemsEvent;
import xyz.nucleoid.stimuli.event.entity.EntityDropItemsEvent;
import xyz.nucleoid.stimuli.event.world.ExplosionDetonatedEvent;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -57,6 +75,7 @@ public boolean enableModule(RegistryEntry<Module> module) {
if (modules.contains(module)) {
return false;
}

//TODO: trigger the modifier (they may have something to do when enabled)
//TODO: send feedback to all players in game (chat + title)

Expand Down Expand Up @@ -141,6 +160,59 @@ public static <V extends Modifier> Stream<V> streamModifiers(Stream<RegistryEntr
.map(modifier -> (V) modifier);
}

// LISTENERS
public void setupListeners(GameActivity activity) {
activity.listen(EntityDropItemsEvent.EVENT, this::onMobLoot);
activity.listen(BlockBreakEvent.EVENT, this::onBlockBroken);
activity.listen(BlockDropItemsEvent.EVENT, this::onBlockDrop);
activity.listen(ExplosionDetonatedEvent.EVENT, this::onExplosion);
}

private EventResult onBlockBroken(ServerPlayerEntity playerEntity, ServerWorld world, BlockPos pos) {
for (TraversalBreakModifier piece : this.getModifiers(ModifierType.TRAVERSAL_BREAK)) {
piece.breakBlock(world, playerEntity, pos);
}
return EventResult.ALLOW;
}

private EventResult onExplosion(Explosion explosion, List<BlockPos> positions) {
positions.forEach(pos -> {
for (TraversalBreakModifier piece : this.getModifiers(ModifierType.TRAVERSAL_BREAK)) {
piece.breakBlock(explosion.getWorld(), explosion.getCausingEntity(), pos);
}
});
return EventResult.ALLOW;
}


private DroppedItemsResult onMobLoot(LivingEntity livingEntity, List<ItemStack> itemStacks) {
boolean keepOld = true;
List<ItemStack> stacks = new ArrayList<>();
for (EntityLootModifier piece : this.getModifiers(ModifierType.ENTITY_LOOT)) {
if (piece.test(livingEntity)) {
stacks.addAll(piece.getLoots((ServerWorld) livingEntity.getWorld(), livingEntity));
if (piece.shouldReplace()) keepOld = false;
}
}
if (keepOld) stacks.addAll(itemStacks);
return DroppedItemsResult.pass(stacks);
}

private DroppedItemsResult onBlockDrop(@Nullable Entity entity, ServerWorld world, BlockPos pos, BlockState state, List<ItemStack> itemStacks) {
boolean keepOld = true;
List<ItemStack> stacks = new ArrayList<>();
for (BlockLootModifier piece : this.getModifiers(ModifierType.BLOCK_LOOT)) {
if (piece.test(state, world.getRandom())) {
piece.spawnExperience(world, pos);
stacks.addAll(piece.getLoots(world, pos, entity, entity instanceof LivingEntity ? ((LivingEntity) entity).getActiveItem() : ItemStack.EMPTY));
if (piece.shouldReplace()) keepOld = false;
}
}
if (keepOld) stacks.addAll(itemStacks);
return DroppedItemsResult.pass(stacks);
}


@Override
public String toString() {
return "ModuleManager[" +
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/hugman/uhc/game/UHCPlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class UHCPlayerManager {
Expand Down Expand Up @@ -92,8 +92,16 @@ public int aliveParticipantCount() {
return (int) participants.values().stream().filter(participant -> !participant.isEliminated()).count();
}

public void forEachParticipant(final BiConsumer<ServerPlayerEntity, UHCParticipant> consumer) {
participants.forEach(consumer);
public void forEachAliveParticipant(final Consumer<ServerPlayerEntity> consumer) {
participants.forEach((player, participant) -> {
if (!participant.isEliminated()) {
consumer.accept(player);
}
});
}

public void forEachParticipant(final Consumer<ServerPlayerEntity> consumer) {
participants.keySet().forEach(consumer);
}

// TEAMS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import com.hugman.uhc.config.UHCGameConfig;

public class UHCLogic {
public class UHCTimers {
private static final int HIGH_PLAYER_COUNT = 100;

private final UHCGameConfig config;
private final float playerDose;

public UHCLogic(UHCGameConfig config, int playerAmount) {
public UHCTimers(UHCGameConfig config, int playerAmount) {
this.config = config;
this.playerDose = (playerAmount - (float) config.players().minPlayers()) / ((float) Math.max(config.players().minPlayers(), HIGH_PLAYER_COUNT) - (float) config.players().minPlayers());
}
Expand Down
Loading

0 comments on commit e2b29d3

Please sign in to comment.