Skip to content

Commit

Permalink
Replace attribute modifier on River Race collectable with a modifier …
Browse files Browse the repository at this point in the history
…applied when the item is in the inventory at all
  • Loading branch information
Gegy committed Nov 11, 2024
1 parent 925fd9b commit 385d804
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/com/lovetropics/minigames/LoveTropics.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public LoveTropics(IEventBus modBus, ModContainer modContainer) {
SoundRegistry.REGISTER.register(modBus);
MinigameDataComponents.REGISTER.register(modBus);
BiodiversityBlitz.DATA_COMPONENTS.register(modBus);
RiverRace.DATA_COMPONENTS.register(modBus);
VoidChunkGenerator.REGISTER.register(modBus);

LoveTropicsEntityOptions.register();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@
import com.tterrag.registrate.providers.ProviderType;
import com.tterrag.registrate.util.entry.BlockEntityEntry;
import com.tterrag.registrate.util.entry.BlockEntry;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.registries.Registries;
import net.minecraft.util.Unit;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;

public class RiverRace {
private static final LoveTropicsRegistrate REGISTRATE = LoveTropics.registrate();

public static final DeferredRegister.DataComponents DATA_COMPONENTS = DeferredRegister.createDataComponents(LoveTropics.ID);

public static final GameBehaviorEntry<TriviaBehaviour> TRIVIA_BEHAVIOUR = REGISTRATE.object("trivia").behavior(TriviaBehaviour.CODEC).register();
public static final GameBehaviorEntry<StartMicrogamesAction> START_MICROGAMES_ACTION = REGISTRATE.object("start_microgames").behavior(StartMicrogamesAction.CODEC).register();
public static final GameBehaviorEntry<VictoryPointsBehavior> VICTORY_POINTS_BEHAVIOR = REGISTRATE.object("victory_points").behavior(VictoryPointsBehavior.CODEC).register();
Expand All @@ -27,7 +34,6 @@ public class RiverRace {
public static final GameBehaviorEntry<CollectablesBehaviour> COLLECTABLES_BEHAVIOUR = REGISTRATE.object("river_race_collectables").behavior(CollectablesBehaviour.CODEC).register();
public static final GameBehaviorEntry<PreventBreakBehaviour> PREVENT_BREAK_BEHAVIOUR = REGISTRATE.object("prevent_break").behavior(PreventBreakBehaviour.CODEC).register();


public static final BlockEntry<TriviaBlock.GateTriviaBlock> TRIVIA_GATE = REGISTRATE
.block("trivia_gate", TriviaBlock.GateTriviaBlock::new)
.initialProperties(() -> Blocks.BEDROCK)
Expand Down Expand Up @@ -79,6 +85,10 @@ public class RiverRace {
.validBlocks(TRIVIA_GATE, TRIVIA_COLLECTABLE, TRIVIA_VICTORY).register();
public static final BlockEntityEntry<TriviaChestBlockEntity> TRIVIA_CHEST_BLOCK_ENTITY = BlockEntityEntry.cast(REGISTRATE.get("trivia_chest", Registries.BLOCK_ENTITY_TYPE));

public static final DeferredHolder<DataComponentType<?>, DataComponentType<Unit>> COLLECTABLE_MARKER = DATA_COMPONENTS.registerComponentType(
"river_race_collectable_marker",
builder -> builder.persistent(Unit.CODEC)
);

public static void init() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.SetRoleTrigger;
import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.TopPlayerTrigger;
import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.WeatherChangeTrigger;
import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.WhileInInventoryTrigger;
import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.WhileInRegionTrigger;
import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.phase.GameReadyTrigger;
import com.lovetropics.minigames.common.core.game.behavior.instances.trigger.phase.GameTickTrigger;
Expand Down Expand Up @@ -271,6 +272,7 @@ public class GameBehaviorTypes {
public static final GameBehaviorEntry<GiveItemsToKillerBehavior> GIVE_ITEMS_TO_KILLER = register("give_items_to_killer", GiveItemsToKillerBehavior.CODEC);
public static final GameBehaviorEntry<CountdownEffectsBehavior> COUNTDOWN_EFFECTS = register("countdown_effects", CountdownEffectsBehavior.CODEC);
public static final GameBehaviorEntry<TriggerEveryPackageBehavior> TRIGGER_EVERY_PACKAGE = register("trigger_every_package", TriggerEveryPackageBehavior.CODEC);
public static final GameBehaviorEntry<WhileInInventoryTrigger> WHILE_IN_INVENTORY = register("while_in_inventory", WhileInInventoryTrigger.CODEC);

public static final GameBehaviorEntry<SetupIntegrationsBehavior> SETUP_INTEGRATIONS = register("setup_integrations", SetupIntegrationsBehavior.CODEC);
public static final GameBehaviorEntry<AssignPlayerRolesBehavior> ASSIGN_PLAYER_ROLES = register("assign_player_roles", AssignPlayerRolesBehavior.CODEC);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.lovetropics.minigames.common.core.game.behavior.instances.trigger;

import com.lovetropics.minigames.common.core.game.IGamePhase;
import com.lovetropics.minigames.common.core.game.behavior.GameBehaviorType;
import com.lovetropics.minigames.common.core.game.behavior.GameBehaviorTypes;
import com.lovetropics.minigames.common.core.game.behavior.IGameBehavior;
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.event.EventRegistrar;
import com.lovetropics.minigames.common.core.game.behavior.event.GamePlayerEvents;
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.minecraft.advancements.critereon.ItemPredicate;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.inventory.CraftingContainer;

import java.util.UUID;
import java.util.function.Supplier;

public record WhileInInventoryTrigger(
ItemPredicate itemPredicate,
GameActionList<ServerPlayer> apply,
GameActionList<ServerPlayer> clear,
boolean stack
) implements IGameBehavior {
public static final MapCodec<WhileInInventoryTrigger> CODEC = RecordCodecBuilder.mapCodec(i -> i.group(
ItemPredicate.CODEC.fieldOf("item").forGetter(WhileInInventoryTrigger::itemPredicate),
GameActionList.PLAYER_CODEC.optionalFieldOf("apply", GameActionList.EMPTY).forGetter(WhileInInventoryTrigger::apply),
GameActionList.PLAYER_CODEC.optionalFieldOf("clear", GameActionList.EMPTY).forGetter(WhileInInventoryTrigger::clear),
Codec.BOOL.optionalFieldOf("stack", false).forGetter(WhileInInventoryTrigger::stack)
).apply(i, WhileInInventoryTrigger::new));

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

Object2IntMap<UUID> countByPlayer = new Object2IntOpenHashMap<>();
events.listen(GamePlayerEvents.TICK, player -> {
int oldCount = countByPlayer.getInt(player.getUUID());
int newCount = countInInventory(player);
if (newCount != oldCount) {
countByPlayer.put(player.getUUID(), newCount);
onItemCountChanged(game, player, oldCount, newCount);
}
});
}

private void onItemCountChanged(IGamePhase game, ServerPlayer player, int oldCount, int newCount) {
if (!stack) {
oldCount = Math.min(oldCount, 1);
newCount = Math.min(newCount, 1);
}

if (newCount > oldCount) {
for (int i = 0; i < newCount - oldCount; i++) {
apply.apply(game, GameActionContext.EMPTY, player);
}
} else {
for (int i = 0; i < oldCount - newCount; i++) {
clear.apply(game, GameActionContext.EMPTY, player);
}
}
}

private int countInInventory(ServerPlayer player) {
CraftingContainer craftSlots = player.inventoryMenu.getCraftSlots();
return player.getInventory().clearOrCountMatchingItems(itemPredicate, 0, craftSlots);
}

@Override
public Supplier<? extends GameBehaviorType<?>> behaviorType() {
return GameBehaviorTypes.WHILE_IN_INVENTORY;
}
}

0 comments on commit 385d804

Please sign in to comment.