Skip to content

Commit

Permalink
implement hover mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Erdragh committed Mar 2, 2023
1 parent 9ad1ea3 commit 8ea287b
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ public class JetSuitAdditions implements ModInitializer {

public static final String MODID = "jet_suit_additions";


@Override
public void onInitialize() {
C2SPackets.register();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
});
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}

Expand All @@ -32,9 +40,43 @@ public void appendTooltip(ItemStack stack, World world, List<Text> 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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) -> {
Expand All @@ -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);
}
});
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/assets/jet_suit_additions/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -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"
}
3 changes: 2 additions & 1 deletion src/main/resources/jet_suit_additions.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"package": "com.github.erdragh.jet_suit_additions.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"ItemRegistryMixin"
"ItemRegistryMixin",
"PlayerEntityMixin"
],
"client": [
],
Expand Down

0 comments on commit 8ea287b

Please sign in to comment.