generated from neoforged/MDK
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75325e6
commit cb1e33d
Showing
9 changed files
with
166 additions
and
105 deletions.
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
41 changes: 41 additions & 0 deletions
41
src/main/java/com/leclowndu93150/modular_angelring/common/EnabledModifiersComponent.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,41 @@ | ||
package com.leclowndu93150.modular_angelring.common; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.MapCodec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import net.minecraft.network.RegistryFriendlyByteBuf; | ||
import net.minecraft.network.codec.ByteBufCodecs; | ||
import net.minecraft.network.codec.StreamCodec; | ||
|
||
import java.util.Objects; | ||
|
||
public record EnabledModifiersComponent(boolean inertiaEnabled, boolean speedModifierEnabled, boolean miningEnabled) { | ||
public static final EnabledModifiersComponent EMPTY = new EnabledModifiersComponent(false, false, false); | ||
|
||
public static final MapCodec<EnabledModifiersComponent> CODEC = RecordCodecBuilder.mapCodec(builder -> builder.group( | ||
Codec.BOOL.fieldOf("inertia_enabled").forGetter(EnabledModifiersComponent::inertiaEnabled), | ||
Codec.BOOL.fieldOf("speed_enabled").forGetter(EnabledModifiersComponent::speedModifierEnabled), | ||
Codec.BOOL.fieldOf("mining_enabled").forGetter(EnabledModifiersComponent::miningEnabled) | ||
).apply(builder, EnabledModifiersComponent::new)); | ||
public static final StreamCodec<RegistryFriendlyByteBuf, EnabledModifiersComponent> STREAM_CODEC = StreamCodec.composite( | ||
ByteBufCodecs.BOOL, | ||
EnabledModifiersComponent::inertiaEnabled, | ||
ByteBufCodecs.BOOL, | ||
EnabledModifiersComponent::speedModifierEnabled, | ||
ByteBufCodecs.BOOL, | ||
EnabledModifiersComponent::miningEnabled, | ||
EnabledModifiersComponent::new | ||
); | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof EnabledModifiersComponent that)) return false; | ||
return miningEnabled == that.miningEnabled && inertiaEnabled == that.inertiaEnabled && speedModifierEnabled == that.speedModifierEnabled; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(inertiaEnabled, speedModifierEnabled, miningEnabled); | ||
} | ||
} |
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
38 changes: 26 additions & 12 deletions
38
src/main/java/com/leclowndu93150/modular_angelring/networking/PayloadActions.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 |
---|---|---|
@@ -1,31 +1,45 @@ | ||
package com.leclowndu93150.modular_angelring.networking; | ||
|
||
import com.leclowndu93150.modular_angelring.common.EnabledModifiersComponent; | ||
import com.leclowndu93150.modular_angelring.registry.DataComponentRegistry; | ||
import com.leclowndu93150.modular_angelring.registry.ItemRegistry; | ||
import com.leclowndu93150.modular_angelring.registry.KeyBindRegistry; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.level.Level; | ||
import net.neoforged.neoforge.network.PacketDistributor; | ||
import net.neoforged.neoforge.network.handling.IPayloadContext; | ||
import top.theillusivec4.curios.api.CuriosApi; | ||
import top.theillusivec4.curios.api.SlotResult; | ||
|
||
import java.util.Optional; | ||
|
||
public final class PayloadActions { | ||
public static boolean inertiaEnabled = true; | ||
public static boolean speedEnabled = true; | ||
public static boolean isNoKeysPressed = false; | ||
|
||
public static final String NO_KEYS_PRESSED = "no_keys_pressed"; | ||
|
||
public static void keyPressedAction(KeyPressedPayload payload, IPayloadContext ctx) { | ||
// SERVER-SIDE | ||
Player player = ctx.player(); | ||
Level level = player.level(); | ||
int key = payload.key(); | ||
if (key == KeyBindRegistry.INERTIA_MODULE.get().getKey().getValue()) { | ||
inertiaEnabled = !inertiaEnabled; | ||
} | ||
|
||
if (key == KeyBindRegistry.SPEED_MODULE.get().getKey().getValue()) { | ||
speedEnabled = !speedEnabled; | ||
Optional<SlotResult> slotResult = CuriosApi.getCuriosInventory(player).flatMap(handler -> handler.findFirstCurio(ItemRegistry.ANGEL_RING.get())); | ||
if (slotResult.isPresent()) { | ||
ItemStack itemStack = slotResult.get().stack(); | ||
EnabledModifiersComponent data = itemStack.getOrDefault(DataComponentRegistry.MODIFIERS_ENABLED, EnabledModifiersComponent.EMPTY); | ||
if (key == KeyBindRegistry.INERTIA_MODULE.get().getKey().getValue()) { | ||
itemStack.set(DataComponentRegistry.MODIFIERS_ENABLED, | ||
new EnabledModifiersComponent(!data.inertiaEnabled(), data.speedModifierEnabled(), data.miningEnabled())); | ||
} else if (key == KeyBindRegistry.SPEED_MODULE.get().getKey().getValue()) { | ||
itemStack.set(DataComponentRegistry.MODIFIERS_ENABLED, | ||
new EnabledModifiersComponent(data.inertiaEnabled(), !data.speedModifierEnabled(), data.miningEnabled())); | ||
} | ||
} | ||
} | ||
|
||
public static void noKeyPressedAction(noKeyPressedPayload payload, IPayloadContext ctx) { | ||
public static void noKeyPressedAction(NoKeyPressedPayload payload, IPayloadContext ctx) { | ||
// SERVER-SIDE | ||
isNoKeysPressed = payload.pressed(); | ||
Player player = ctx.player(); | ||
player.getPersistentData().putBoolean(NO_KEYS_PRESSED, payload.pressed()); | ||
} | ||
} |
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
Oops, something went wrong.