diff --git a/src/main/java/com/github/erdragh/jet_suit_additions/JetSuitAdditions.java b/src/main/java/com/github/erdragh/jet_suit_additions/JetSuitAdditions.java index fdc370f..04ac370 100644 --- a/src/main/java/com/github/erdragh/jet_suit_additions/JetSuitAdditions.java +++ b/src/main/java/com/github/erdragh/jet_suit_additions/JetSuitAdditions.java @@ -7,8 +7,11 @@ public class JetSuitAdditions implements ModInitializer { public static final String MODID = "jet_suit_additions"; + @Override public void onInitialize() { C2SPackets.register(); } + + } diff --git a/src/main/java/com/github/erdragh/jet_suit_additions/client/JetSuitAdditionsClient.java b/src/main/java/com/github/erdragh/jet_suit_additions/client/JetSuitAdditionsClient.java index 8323573..3e7ca30 100644 --- a/src/main/java/com/github/erdragh/jet_suit_additions/client/JetSuitAdditionsClient.java +++ b/src/main/java/com/github/erdragh/jet_suit_additions/client/JetSuitAdditionsClient.java @@ -18,16 +18,20 @@ public class JetSuitAdditionsClient implements ClientModInitializer { public static final String KEY_CATEGORY = JetSuitAdditions.MODID + ".key_category"; - public static KeyBinding jetSuitToggle; + public static KeyBinding jetSuitToggle, jetSuitHoverToggle; @Override public void onInitializeClient() { jetSuitToggle = KeyBindingHelper.registerKeyBinding(new KeyBinding(JetSuitAdditions.MODID + ".key.toggle_jet_suit", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_COMMA, KEY_CATEGORY)); + jetSuitHoverToggle = KeyBindingHelper.registerKeyBinding(new KeyBinding(JetSuitAdditions.MODID + ".key.toggle_jet_suit_hover", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_PERIOD, KEY_CATEGORY)); ClientTickEvents.END_CLIENT_TICK.register(client -> { while (jetSuitToggle.wasPressed()) { ClientPlayNetworking.send(C2SPackets.TOGGLE_ON, PacketByteBufs.create()); } + while (jetSuitHoverToggle.wasPressed()) { + ClientPlayNetworking.send(C2SPackets.TOGGLE_HOVER, PacketByteBufs.create()); + } }); } } diff --git a/src/main/java/com/github/erdragh/jet_suit_additions/items/armour/ImprovedJetSuit.java b/src/main/java/com/github/erdragh/jet_suit_additions/items/armour/ImprovedJetSuit.java index 4635e04..a576be5 100644 --- a/src/main/java/com/github/erdragh/jet_suit_additions/items/armour/ImprovedJetSuit.java +++ b/src/main/java/com/github/erdragh/jet_suit_additions/items/armour/ImprovedJetSuit.java @@ -1,7 +1,9 @@ package com.github.erdragh.jet_suit_additions.items.armour; +import com.github.alexnijjar.ad_astra.AdAstra; import com.github.alexnijjar.ad_astra.items.armour.JetSuit; import com.github.alexnijjar.ad_astra.registry.ModItems; +import com.github.alexnijjar.ad_astra.util.ModKeyBindings; import com.github.erdragh.jet_suit_additions.JetSuitAdditions; import net.minecraft.client.item.TooltipContext; import net.minecraft.entity.EquipmentSlot; @@ -17,13 +19,19 @@ import java.util.List; public class ImprovedJetSuit extends JetSuit { + + public static final int HOVER_SINK_SPEED = 1; public ImprovedJetSuit(ArmorMaterial material, EquipmentSlot slot, Settings settings) { super(material, slot, settings); } @Override public void fly(PlayerEntity player, ItemStack stack) { - if (!stack.getOrCreateNbt().getBoolean("toggle_on")) return; + // Don't fly if the Jet Suit is disabled + if (!stack.getOrCreateNbt().getBoolean("toggle_on")) { + stack.getOrCreateNbt().putBoolean("spawn_particles", false); + return; + } super.fly(player, stack); } @@ -32,9 +40,43 @@ public void appendTooltip(ItemStack stack, World world, List tooltip, Tool super.appendTooltip(stack, world, tooltip, context); if (stack.isOf(ModItems.JET_SUIT)) { boolean turnedOn = stack.getOrCreateNbt().getBoolean("toggle_on"); - Text text = new TranslatableText(JetSuitAdditions.MODID + ".msg.jet_suit_toggle").append(new TranslatableText(JetSuitAdditions.MODID + ".msg.jet_suit_" + (turnedOn ? "on" : "off")).setStyle(Style.EMPTY.withBold(true).withColor(turnedOn ? Formatting.GREEN : Formatting.RED))); + boolean hoverOn = stack.getOrCreateNbt().getBoolean("toggle_hover"); + Text turnedOnText = new TranslatableText(JetSuitAdditions.MODID + ".msg.jet_suit_toggle").append(new TranslatableText(JetSuitAdditions.MODID + ".msg.jet_suit_" + (turnedOn ? "on" : "off")).setStyle(Style.EMPTY.withBold(true).withColor(turnedOn ? Formatting.GREEN : Formatting.RED))); + Text hoverOnText = new TranslatableText(JetSuitAdditions.MODID + ".msg.jet_suit_toggle_hover").append(new TranslatableText(JetSuitAdditions.MODID + ".msg.jet_suit_" + (hoverOn ? "on" : "off")).setStyle(Style.EMPTY.withBold(true).withColor(hoverOn ? Formatting.GREEN : Formatting.RED))); + tooltip.add(turnedOnText); + tooltip.add(hoverOnText); + } + } + + @Override + public void hover(PlayerEntity player, ItemStack stack) { + if (ModKeyBindings.jumpKeyDown(player)) { + super.hover(player, stack); + } else { + double speed; + if (player.isSneaking()) { + speed = -AdAstra.CONFIG.spaceSuit.jetSuitUpwardsSpeed; + } else { + speed = -player.getVelocity().getY() / HOVER_SINK_SPEED; + } + if (!player.isOnGround()) { + hover(player, stack, speed); + } else { + stack.getOrCreateNbt().putBoolean("spawn_particles", false); + } + } + } + + private void hover(PlayerEntity player, ItemStack stack, double speed) { + player.fallDistance /= 2; + if (!player.isCreative() && !this.tryUseEnergy(stack, AdAstra.CONFIG.spaceSuit.jetSuitEnergyPerTick)) { + this.setStoredEnergy(stack, 0); + } + isFallFlying = false; - tooltip.add(text); + player.setVelocity(player.getVelocity().add(0.0, speed, 0.0)); + if (Math.abs(player.getVelocity().getY()) > Math.abs(speed)) { + player.setVelocity(player.getVelocity().getX(), speed, player.getVelocity().getZ()); } } } diff --git a/src/main/java/com/github/erdragh/jet_suit_additions/mixin/PlayerEntityMixin.java b/src/main/java/com/github/erdragh/jet_suit_additions/mixin/PlayerEntityMixin.java new file mode 100644 index 0000000..adb503e --- /dev/null +++ b/src/main/java/com/github/erdragh/jet_suit_additions/mixin/PlayerEntityMixin.java @@ -0,0 +1,28 @@ +package com.github.erdragh.jet_suit_additions.mixin; + +import com.github.alexnijjar.ad_astra.AdAstra; +import com.github.alexnijjar.ad_astra.util.ModKeyBindings; +import com.github.erdragh.jet_suit_additions.items.armour.ImprovedJetSuit; +import net.minecraft.entity.EquipmentSlot; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(PlayerEntity.class) +public class PlayerEntityMixin { + @Inject(method = "tick", at = @At("TAIL")) + public void jet_suit_additions_tick(CallbackInfo ci) { + if (AdAstra.CONFIG.spaceSuit.enableJetSuitFlight) { + PlayerEntity player = ((PlayerEntity) (Object) this); + if (!player.hasVehicle() && !ModKeyBindings.jumpKeyDown(player)) { + ItemStack chest = player.getEquippedStack(EquipmentSlot.CHEST); + if (chest.getItem() instanceof ImprovedJetSuit jetSuit && ImprovedJetSuit.hasFullSet(player) && chest.getOrCreateNbt().getBoolean("toggle_hover")) { + jetSuit.fly(player, chest); + } + } + } + } +} diff --git a/src/main/java/com/github/erdragh/jet_suit_additions/networking/C2SPackets.java b/src/main/java/com/github/erdragh/jet_suit_additions/networking/C2SPackets.java index 68eba9a..759cce3 100644 --- a/src/main/java/com/github/erdragh/jet_suit_additions/networking/C2SPackets.java +++ b/src/main/java/com/github/erdragh/jet_suit_additions/networking/C2SPackets.java @@ -11,6 +11,8 @@ public class C2SPackets { public static final Identifier TOGGLE_ON = new Identifier(JetSuitAdditions.MODID, "toggle_on"); + public static final Identifier TOGGLE_HOVER = new Identifier(JetSuitAdditions.MODID, "toggle_hover"); + public static void register() { ServerPlayNetworking.registerGlobalReceiver(TOGGLE_ON, (server, player, handler, buf, responseSender) -> { @@ -19,6 +21,15 @@ public static void register() { chestStack.getOrCreateNbt().putBoolean("toggle_on", !chestStack.getOrCreateNbt().getBoolean("toggle_on")); Text text = new TranslatableText(JetSuitAdditions.MODID + ".msg.jet_suit_toggle").append(new TranslatableText(JetSuitAdditions.MODID + ".msg.jet_suit_" + (chestStack.getOrCreateNbt().getBoolean("toggle_on") ? "on" : "off"))); + player.sendMessage(text, true); + } + }); + ServerPlayNetworking.registerGlobalReceiver(TOGGLE_HOVER, (server, player, handler, buf, responseSender) -> { + var chestStack = player.getInventory().getArmorStack(EquipmentSlot.CHEST.getEntitySlotId()); + if (chestStack.getItem() instanceof ImprovedJetSuit) { + chestStack.getOrCreateNbt().putBoolean("toggle_hover", !chestStack.getOrCreateNbt().getBoolean("toggle_hover")); + Text text = new TranslatableText(JetSuitAdditions.MODID + ".msg.jet_suit_toggle_hover").append(new TranslatableText(JetSuitAdditions.MODID + ".msg.jet_suit_" + (chestStack.getOrCreateNbt().getBoolean("toggle_hover") ? "on" : "off"))); + player.sendMessage(text, true); } }); diff --git a/src/main/resources/assets/jet_suit_additions/lang/en_us.json b/src/main/resources/assets/jet_suit_additions/lang/en_us.json index 7a06875..346d37d 100644 --- a/src/main/resources/assets/jet_suit_additions/lang/en_us.json +++ b/src/main/resources/assets/jet_suit_additions/lang/en_us.json @@ -1,8 +1,10 @@ { "jet_suit_additions.msg.jet_suit_toggle": "Jet Suit turned ", + "jet_suit_additions.msg.jet_suit_toggle_hover": "Jet Suit Hover turned ", "jet_suit_additions.msg.jet_suit_on": "on", "jet_suit_additions.msg.jet_suit_off": "off", "jet_suit_additions.key_category": "Jet Suit Additions", - "jet_suit_additions.key.toggle_jet_suit": "Turn the Jet Suit on/off" + "jet_suit_additions.key.toggle_jet_suit": "Turn the Jet Suit on/off", + "jet_suit_additions.key.toggle_jet_suit_hover": "Turn Hover Mode on/off" } \ No newline at end of file diff --git a/src/main/resources/jet_suit_additions.mixins.json b/src/main/resources/jet_suit_additions.mixins.json index 3054992..14b1cc3 100644 --- a/src/main/resources/jet_suit_additions.mixins.json +++ b/src/main/resources/jet_suit_additions.mixins.json @@ -4,7 +4,8 @@ "package": "com.github.erdragh.jet_suit_additions.mixin", "compatibilityLevel": "JAVA_17", "mixins": [ - "ItemRegistryMixin" + "ItemRegistryMixin", + "PlayerEntityMixin" ], "client": [ ],