Skip to content

Commit

Permalink
feat: add the ability to toggle being a ninja
Browse files Browse the repository at this point in the history
  • Loading branch information
sekwah41 committed Sep 20, 2022
1 parent 301863b commit 00df488
Show file tree
Hide file tree
Showing 14 changed files with 245 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ public interface INinjaData extends INBTSerializable<Tag> {
* @param player
*/
void updateDataClient(Player player);

void setIsNinja(boolean enableNinja);

boolean isNinjaModeEnabled();
}
27 changes: 25 additions & 2 deletions src/main/java/com/sekwah/narutomod/capabilities/NinjaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public class NinjaData implements INinjaData, ICapabilityProvider {
@Sync
private float maxChakra;

/**
* If the player should have access to all the ninja shit
*/
@Sync(syncGlobally = true)
private boolean ninjaModeEnabled;

@Sync
private float maxStamina;

Expand Down Expand Up @@ -87,9 +93,9 @@ public class NinjaData implements INinjaData, ICapabilityProvider {
* This will make the player truly invisible.
*/
@Sync(minTicks = 1, syncGlobally = true)
private boolean isInvisible;
private boolean isInvisible = false;

private int invisibleTicks;
private int invisibleTicks = 0;


private ArrayList<DelayedPlayerTickEvent> delayedTickEvents = new ArrayList<>();
Expand Down Expand Up @@ -131,6 +137,7 @@ public boolean canRegen() {

private static final String CHAKRA_TAG = "chakra";
private static final String STAMINA_TAG = "stamina";
private static final String NINJA_MODE_ENABLED = "ninjaModeEnabled";
private static final String SAVE_TIME = "save_time";
private static final String COOLDOWN_TAG = "cooldowns";
private static final String SUBSTITUTION_TAG = "substitutions";
Expand Down Expand Up @@ -291,6 +298,10 @@ public void updateDataServer(Player player) {
this.isInvisible = false;
}

if(!this.isNinjaModeEnabled()) {
return;
}

this.getConfigData();
Iterator<DelayedPlayerTickEvent> iterator = this.delayedTickEvents.iterator();
while (iterator.hasNext()) {
Expand Down Expand Up @@ -365,11 +376,22 @@ public void updateDataClient(Player player) {
this.doubleJumpData.stuckCheck();
}

@Override
public void setIsNinja(boolean enableNinja) {
this.ninjaModeEnabled = enableNinja;
}

@Override
public boolean isNinjaModeEnabled() {
return this.ninjaModeEnabled;
}

@Override
public Tag serializeNBT() {
final CompoundTag nbt = new CompoundTag();
nbt.putFloat(CHAKRA_TAG, this.chakra);
nbt.putFloat(STAMINA_TAG, this.stamina);
nbt.putBoolean(NINJA_MODE_ENABLED, this.ninjaModeEnabled);
long currentTime = System.currentTimeMillis();
nbt.putLong(SAVE_TIME, currentTime);
final CompoundTag cooldownData = new CompoundTag();
Expand All @@ -390,6 +412,7 @@ public void deserializeNBT(Tag tag) {
int ticksPassed = Math.max((int) ((currentTime - saveTime) / 1000 * 20), 0);
this.chakra = compoundTag.getFloat(CHAKRA_TAG);
this.stamina = compoundTag.getFloat(STAMINA_TAG);
this.ninjaModeEnabled = compoundTag.getBoolean(NINJA_MODE_ENABLED);
CompoundTag cooldownData = compoundTag.getCompound(COOLDOWN_TAG);
for (String key : cooldownData.getAllKeys()) {
this.cooldownTickEvents.put(key, new CooldownTickEvent(cooldownData.getInt(key) - ticksPassed));
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/com/sekwah/narutomod/client/gui/JutsuScreen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.sekwah.narutomod.client.gui;

import com.mojang.blaze3d.vertex.PoseStack;
import com.sekwah.narutomod.capabilities.NinjaCapabilityHandler;
import com.sekwah.narutomod.network.PacketHandler;
import com.sekwah.narutomod.network.c2s.ServerToggleNinjaPacket;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;

import java.util.concurrent.atomic.AtomicBoolean;

public class JutsuScreen extends Screen {
public JutsuScreen() {
super(Component.translatable("naruto.gui.jutsu.title"));
}

private Button becomeANinja;
private Button changeBack;

private Component[] renderLines = {
Component.translatable("naruto.gui.jutsu.notice"),
Component.translatable("naruto.gui.jutsu.notice2"),
Component.empty(),
Component.translatable("naruto.gui.jutsu.placeholder"),
};

protected void init() {
this.addButtons();
}

public void addButtons() {

becomeANinja = this.addRenderableWidget(new Button(this.width / 2 - 102, this.height / 4 + 72 + -16, 98, 20, Component.translatable("naruto.gui.jutsu.enable"), (buttonRef) -> {
PacketHandler.sendToServer(new ServerToggleNinjaPacket(true));
}));
renderables.add(becomeANinja);
becomeANinja.active = false;

changeBack = this.addRenderableWidget(new Button(this.width / 2 + 4, this.height / 4 + 72 + -16, 98, 20, Component.translatable("naruto.gui.jutsu.disable"), (buttonRef) -> {
PacketHandler.sendToServer(new ServerToggleNinjaPacket(false));
}));
renderables.add(changeBack);
changeBack.active = false;

Button back = this.addRenderableWidget(new Button(this.width / 2 - 98 / 2, this.height / 4 + 72 + 7, 98, 20, Component.translatable("naruto.gui.jutsu.done"), (buttonRef) -> {
this.minecraft.popGuiLayer();
}));
renderables.add(back);
}

public void tick() {
super.tick();
AtomicBoolean isNinja = new AtomicBoolean(false);
var player = this.minecraft.player;
if(player != null) {
player.getCapability(NinjaCapabilityHandler.NINJA_DATA).ifPresent(ninjaData -> {
isNinja.set(ninjaData.isNinjaModeEnabled());
});
}
becomeANinja.active = !isNinja.get();
changeBack.active = isNinja.get();
}

public boolean isPauseScreen() {
return false;
}

public void render(PoseStack poseStack, int p_96311_, int p_96312_, float p_96313_) {
this.renderBackground(poseStack);
drawCenteredString(poseStack, this.font, this.title, this.width / 2, 40, 16777215);
for (int i = 0; i < renderLines.length; i++) {
drawCenteredString(poseStack, this.font, renderLines[i], this.width / 2, 60 + 10 * i, 16777215);
}
super.render(poseStack, p_96311_, p_96312_, p_96313_);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Matrix4f;
import com.sekwah.narutomod.capabilities.NinjaCapabilityHandler;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.world.entity.player.Player;
Expand Down Expand Up @@ -59,7 +60,9 @@ public static void registerEvents() {
@SubscribeEvent
public void clientTickEvent(TickEvent.ClientTickEvent event) {
if(this.minecraft.getCameraEntity() instanceof Player player) {
shouldRender = true;
player.getCapability(NinjaCapabilityHandler.NINJA_DATA).ifPresent(ninjaData -> {
shouldRender = ninjaData.isNinjaModeEnabled();
});
for (PlayerGUI overlay : overlays) {
overlay.tick(player);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.sekwah.narutomod.NarutoMod;
import com.sekwah.narutomod.abilities.Ability;
import com.sekwah.narutomod.abilities.NarutoAbilities;
import com.sekwah.narutomod.capabilities.NinjaCapabilityHandler;
import com.sekwah.narutomod.client.gui.JutsuScreen;
import com.sekwah.narutomod.config.NarutoConfig;
import com.sekwah.narutomod.network.PacketHandler;
import com.sekwah.narutomod.network.c2s.ServerAbilityChannelPacket;
Expand Down Expand Up @@ -84,7 +86,8 @@ public static void registerKeyBinds(RegisterKeyMappingsEvent event) {
JUTSU_MENU_KEY.registerClickConsumer( () -> {
Minecraft mc = Minecraft.getInstance();
if(mc.player != null ) {
mc.player.displayClientMessage(Component.translatable("naruto.gui.jutsu.placeholder"), true);
mc.setScreen(new JutsuScreen());
//mc.player.displayClientMessage(Component.translatable("naruto.gui.jutsu.placeholder"), true);
}
});

Expand Down Expand Up @@ -113,15 +116,26 @@ public static void handleJustuKey(int i) {
if(isCurrentlyChargingAbility) {
return;
}
PacketHandler.sendToServer(new ServerJutsuCastingPacket(i));
ticksSinceLastKey = 0;
if (currentJutsuCombo < MAX_JUTSU_VALUE) {
currentJutsuCombo *= 10;
currentJutsuCombo += i;
currentJutsuComboAbility = NarutoAbilities.getAbilityFromCombo(currentJutsuCombo);
} else {
LOGGER.info("Combo too long, ignoring keypress");
Minecraft mc = Minecraft.getInstance();
if(mc.player == null ) {
return;
}
var player = mc.player;
player.getCapability(NinjaCapabilityHandler.NINJA_DATA).ifPresent(ninjaData -> {
if (!ninjaData.isNinjaModeEnabled()) {
mc.player.displayClientMessage(Component.translatable("jutsu.not_a_ninja").withStyle(ChatFormatting.RED), true);
return;
}
PacketHandler.sendToServer(new ServerJutsuCastingPacket(i));
ticksSinceLastKey = 0;
if (currentJutsuCombo < MAX_JUTSU_VALUE) {
currentJutsuCombo *= 10;
currentJutsuCombo += i;
currentJutsuComboAbility = NarutoAbilities.getAbilityFromCombo(currentJutsuCombo);
} else {
LOGGER.info("Combo too long, ignoring keypress");
}
});
}

/**
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/com/sekwah/narutomod/events/PlayerEvents.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.sekwah.narutomod.events;

import com.sekwah.narutomod.NarutoMod;
import com.sekwah.narutomod.capabilities.NinjaCapabilityHandler;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.event.entity.living.LivingFallEvent;
Expand All @@ -20,15 +23,20 @@ public static void onEntityUpdate(LivingEvent.LivingTickEvent event) {
@SubscribeEvent
public static void livingFall(LivingFallEvent event) {
if (event.getEntity() instanceof Player player){
float distance = event.getDistance();
if(distance < 9){
distance *= 0.3f;
}
if(distance > 3) {
distance -= 5f;
distance *= 0.6f;
}
event.setDistance(distance);
player.getCapability(NinjaCapabilityHandler.NINJA_DATA).ifPresent(ninjaData -> {
if (!ninjaData.isNinjaModeEnabled()) {
return;
}
float distance = event.getDistance();
if(distance < 9){
distance *= 0.3f;
}
if(distance > 3) {
distance -= 5f;
distance *= 0.6f;
}
event.setDistance(distance);
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public void aiStep(CallbackInfo ci) {
ItemStack itemstack = this.getItemBySlot(EquipmentSlot.CHEST);
if (!itemstack.canElytraFly(this) && !this.onGround && !this.isFallFlying() && !this.isInWater() && !this.hasEffect(MobEffects.LEVITATION)) {
this.getCapability(NINJA_DATA).ifPresent(ninjaData -> {
if(!ninjaData.isNinjaModeEnabled()) {
return;
}
DoubleJumpData doubleJumpData = ninjaData.getDoubleJumpData();
if(doubleJumpData != null) {
if(doubleJumpData.canDoubleJumpClient && doubleJumpData.diffUpdateTicksClient > 5) {
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/com/sekwah/narutomod/network/PacketHandler.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.sekwah.narutomod.network;

import com.sekwah.narutomod.NarutoMod;
import com.sekwah.narutomod.network.c2s.ServerAbilityActivatePacket;
import com.sekwah.narutomod.network.c2s.ServerAbilityChannelPacket;
import com.sekwah.narutomod.network.c2s.ServerJutsuCastingPacket;
import com.sekwah.narutomod.network.c2s.ServerTestPacket;
import com.sekwah.narutomod.network.c2s.*;
import com.sekwah.narutomod.network.s2c.ClientTestPacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
Expand Down Expand Up @@ -52,10 +49,10 @@ public static void init() {
NARUTO_CHANNEL.registerMessage(getPacketID(), ClientTestPacket.class, ClientTestPacket::encode, ClientTestPacket::decode, ClientTestPacket.Handler::handle);

// Client to server packets
NARUTO_CHANNEL.registerMessage(getPacketID(), ServerTestPacket.class, ServerTestPacket::encode, ServerTestPacket::decode, ServerTestPacket.Handler::handle);
NARUTO_CHANNEL.registerMessage(getPacketID(), ServerJutsuCastingPacket.class, ServerJutsuCastingPacket::encode, ServerJutsuCastingPacket::decode, ServerJutsuCastingPacket.Handler::handle);
NARUTO_CHANNEL.registerMessage(getPacketID(), ServerAbilityActivatePacket.class, ServerAbilityActivatePacket::encode, ServerAbilityActivatePacket::decode, ServerAbilityActivatePacket.Handler::handle);
NARUTO_CHANNEL.registerMessage(getPacketID(), ServerAbilityChannelPacket.class, ServerAbilityChannelPacket::encode, ServerAbilityChannelPacket::decode, ServerAbilityChannelPacket.Handler::handle);
NARUTO_CHANNEL.registerMessage(getPacketID(), ServerToggleNinjaPacket.class, ServerToggleNinjaPacket::encode, ServerToggleNinjaPacket::decode, ServerToggleNinjaPacket.Handler::handle);
}

private static int packetId = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ public static void handle(ServerAbilityActivatePacket msg, Supplier<NetworkEvent
return;
}
player.getCapability(NinjaCapabilityHandler.NINJA_DATA).ifPresent(ninjaData -> {

if(!ninjaData.isNinjaModeEnabled()) {
player.displayClientMessage(Component.translatable("jutsu.not_a_ninja").withStyle(ChatFormatting.RED), true);
return;
}
Ability ability = NarutoRegistries.ABILITIES.getValue(msg.abilityId);
if (ability.activationType() == Ability.ActivationType.INSTANT) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public static void handle(ServerAbilityChannelPacket msg, Supplier<NetworkEvent.
return;
}
player.getCapability(NinjaCapabilityHandler.NINJA_DATA).ifPresent(ninjaData -> {
if(!ninjaData.isNinjaModeEnabled()) {
player.displayClientMessage(Component.translatable("jutsu.not_a_ninja").withStyle(ChatFormatting.RED), true);
return;
}
Ability ability = NarutoRegistries.ABILITIES.getValue(msg.abilityResource);
if(ability == null) {
LOGGER.error("Ability doesnt exist {}", msg.abilityResource);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.sekwah.narutomod.network.c2s;

import com.sekwah.narutomod.capabilities.NinjaCapabilityHandler;
import com.sekwah.narutomod.gameevents.NarutoGameEvents;
import com.sekwah.narutomod.sounds.NarutoSounds;
import net.minecraft.ChatFormatting;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
Expand Down Expand Up @@ -36,21 +39,27 @@ public static void handle(ServerJutsuCastingPacket msg, Supplier<NetworkEvent.Co
ctx.get().enqueueWork(() -> {
ServerPlayer player = ctx.get().getSender();
if(player != null) {
if(player.gameMode.getGameModeForPlayer() == GameType.SPECTATOR) {
return;
}
SoundEvent playSound = switch (msg.jutsuKey) {
case 1 -> NarutoSounds.SEAL_A.get();
case 2 -> NarutoSounds.SEAL_B.get();
case 3 -> NarutoSounds.SEAL_C.get();
default -> null;
};
if(playSound != null) {
player.getCommandSenderWorld().playSound(null,
player.getX(), player.getY(), player.getZ(),
playSound, SoundSource.PLAYERS, 1.0f, 1.0f);
player.getLevel().gameEvent(player, NarutoGameEvents.JUTSU_CASTING.get(), player.position().add(0, player.getEyeHeight() * 0.7, 0));
}
player.getCapability(NinjaCapabilityHandler.NINJA_DATA).ifPresent(ninjaData -> {
if (!ninjaData.isNinjaModeEnabled()) {
player.displayClientMessage(Component.translatable("jutsu.not_a_ninja").withStyle(ChatFormatting.RED), true);
return;
}
if(player.gameMode.getGameModeForPlayer() == GameType.SPECTATOR) {
return;
}
SoundEvent playSound = switch (msg.jutsuKey) {
case 1 -> NarutoSounds.SEAL_A.get();
case 2 -> NarutoSounds.SEAL_B.get();
case 3 -> NarutoSounds.SEAL_C.get();
default -> null;
};
if(playSound != null) {
player.getCommandSenderWorld().playSound(null,
player.getX(), player.getY(), player.getZ(),
playSound, SoundSource.PLAYERS, 1.0f, 1.0f);
player.getLevel().gameEvent(player, NarutoGameEvents.JUTSU_CASTING.get(), player.position().add(0, player.getEyeHeight() * 0.7, 0));
}
});
}
});
ctx.get().setPacketHandled(true);
Expand Down

0 comments on commit 00df488

Please sign in to comment.