-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace attribute modifier on River Race collectable with a modifier …
…applied when the item is in the inventory at all
- Loading branch information
Showing
4 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...ropics/minigames/common/core/game/behavior/instances/trigger/WhileInInventoryTrigger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |