Skip to content

Commit e1b149d

Browse files
committed
feat: Add Quick-Teleport keybind
1 parent 7cd32d6 commit e1b149d

File tree

6 files changed

+151
-0
lines changed

6 files changed

+151
-0
lines changed

src/main/java/dev/tonimatas/mekanismcurios/MekanismCurios.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.mojang.logging.LogUtils;
44
import dev.tonimatas.mekanismcurios.bridge.PlayerBridge;
55
import dev.tonimatas.mekanismcurios.networking.OpenPortableQIOPacket;
6+
import dev.tonimatas.mekanismcurios.networking.QuickTeleportActionPacket;
67
import dev.tonimatas.mekanismcurios.util.CuriosSlots;
78
import mekanism.common.registries.MekanismItems;
89
import net.minecraft.server.level.ServerPlayer;
@@ -45,6 +46,7 @@ public void registerCapabilities(final RegisterCapabilitiesEvent event) {
4546
public void registerNetworking(final RegisterPayloadHandlersEvent event) {
4647
final PayloadRegistrar registrar = event.registrar("1");
4748
registrar.commonToServer(OpenPortableQIOPacket.TYPE, OpenPortableQIOPacket.STREAM_CODEC, new MainThreadPayloadHandler<>(OpenPortableQIOPacket::handle));
49+
registrar.commonToServer(QuickTeleportActionPacket.TYPE, QuickTeleportActionPacket.STREAM_CODEC, new MainThreadPayloadHandler<>(QuickTeleportActionPacket::handle));
4850
}
4951

5052
public static ItemStack getSlot(Player player) {

src/main/java/dev/tonimatas/mekanismcurios/MekanismCuriosClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.tonimatas.mekanismcurios;
22

33
import dev.tonimatas.mekanismcurios.networking.OpenPortableQIOPacket;
4+
import dev.tonimatas.mekanismcurios.networking.QuickTeleportActionPacket;
45
import dev.tonimatas.mekanismcurios.util.CuriosSlots;
56
import dev.tonimatas.mekanismcurios.util.KeyBinding;
67
import net.neoforged.api.distmarker.Dist;
@@ -22,6 +23,10 @@ public static void onClientTick(ClientTickEvent.Post event) {
2223
while (KeyBinding.PORTABLE_TELEPORTER_MAPPING.get().consumeClick()) {
2324
PacketDistributor.sendToServer(new OpenPortableQIOPacket(CuriosSlots.TELEPORTER));
2425
}
26+
27+
while (KeyBinding.QUICK_TELEPORT_MAPPING.get().consumeClick()) {
28+
PacketDistributor.sendToServer(new QuickTeleportActionPacket());
29+
}
2530
}
2631
}
2732

@@ -31,6 +36,7 @@ public static class ClientModBusEvents {
3136
public static void onKeyRegister(RegisterKeyMappingsEvent event) {
3237
event.register(KeyBinding.PORTABLE_QIO_MAPPING.get());
3338
event.register(KeyBinding.PORTABLE_TELEPORTER_MAPPING.get());
39+
event.register(KeyBinding.QUICK_TELEPORT_MAPPING.get());
3440
}
3541
}
3642
}

src/main/java/dev/tonimatas/mekanismcurios/networking/OpenPortableQIOPacket.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dev.tonimatas.mekanismcurios.MekanismCurios;
44
import dev.tonimatas.mekanismcurios.bridge.PlayerBridge;
55
import dev.tonimatas.mekanismcurios.util.CuriosSlots;
6+
import mekanism.common.item.ItemPortableTeleporter;
67
import mekanism.common.item.interfaces.IGuiItem;
78
import mekanism.common.lib.security.ItemSecurityUtils;
89
import net.minecraft.network.FriendlyByteBuf;
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package dev.tonimatas.mekanismcurios.networking;
2+
3+
import dev.tonimatas.mekanismcurios.MekanismCurios;
4+
import dev.tonimatas.mekanismcurios.util.CuriosSlots;
5+
import mekanism.api.Action;
6+
import mekanism.api.AutomationType;
7+
import mekanism.api.energy.IEnergyContainer;
8+
import mekanism.api.event.MekanismTeleportEvent;
9+
import mekanism.common.attachments.FrequencyAware;
10+
import mekanism.common.content.teleporter.TeleporterFrequency;
11+
import mekanism.common.item.ItemPortableTeleporter;
12+
import mekanism.common.lib.frequency.Frequency;
13+
import mekanism.common.lib.frequency.FrequencyType;
14+
import mekanism.common.network.PacketUtils;
15+
import mekanism.common.network.to_client.PacketPortalFX;
16+
import mekanism.common.registries.MekanismDataComponents;
17+
import mekanism.common.tile.TileEntityTeleporter;
18+
import mekanism.common.util.StorageUtils;
19+
import mekanism.common.util.WorldUtils;
20+
import net.minecraft.core.BlockPos;
21+
import net.minecraft.core.GlobalPos;
22+
import net.minecraft.core.component.DataComponentType;
23+
import net.minecraft.network.FriendlyByteBuf;
24+
import net.minecraft.network.codec.StreamCodec;
25+
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
26+
import net.minecraft.resources.ResourceLocation;
27+
import net.minecraft.server.MinecraftServer;
28+
import net.minecraft.server.level.ServerPlayer;
29+
import net.minecraft.sounds.SoundEvents;
30+
import net.minecraft.sounds.SoundSource;
31+
import net.minecraft.world.item.ItemStack;
32+
import net.minecraft.world.level.Level;
33+
import net.minecraft.world.level.portal.DimensionTransition;
34+
import net.neoforged.neoforge.common.NeoForge;
35+
import net.neoforged.neoforge.network.handling.IPayloadContext;
36+
import org.jetbrains.annotations.NotNull;
37+
38+
public record QuickTeleportActionPacket() implements CustomPacketPayload {
39+
public static final Type<QuickTeleportActionPacket> TYPE =
40+
new Type<>(ResourceLocation.fromNamespaceAndPath(MekanismCurios.MODID, "quick_teleport_action_packet"));
41+
42+
public static final StreamCodec<FriendlyByteBuf, QuickTeleportActionPacket> STREAM_CODEC = StreamCodec.unit(new QuickTeleportActionPacket());
43+
44+
@Override
45+
public @NotNull Type<? extends CustomPacketPayload> type() {
46+
return TYPE;
47+
}
48+
49+
// Modified method from PacketPortableTeleporterTeleport#handle
50+
public static void handle(final QuickTeleportActionPacket packet, final IPayloadContext context) {
51+
context.enqueueWork(() -> {
52+
ServerPlayer player = (ServerPlayer) context.player();
53+
54+
ItemStack stack = CuriosSlots.TELEPORTER.getItemStack(player);
55+
56+
if (!stack.isEmpty() && stack.getItem() instanceof ItemPortableTeleporter item) {
57+
Frequency.FrequencyIdentity identity = getIdentity(item, stack);
58+
59+
if (identity == null) {
60+
return;
61+
}
62+
63+
TeleporterFrequency found = FrequencyType.TELEPORTER.getFrequency(identity, player.getUUID());
64+
if (found == null) {
65+
return;
66+
}
67+
GlobalPos coords = found.getClosestCoords(GlobalPos.of(player.level().dimension(), player.blockPosition()));
68+
if (coords != null) {
69+
MinecraftServer server = player.level().getServer();
70+
Level teleWorld = server == null ? null : server.getLevel(coords.dimension());
71+
TileEntityTeleporter teleporter = WorldUtils.getTileEntity(TileEntityTeleporter.class, teleWorld, coords.pos());
72+
if (teleporter != null) {
73+
long energyCost;
74+
Runnable energyExtraction = null;
75+
if (!player.isCreative()) {
76+
energyCost = TileEntityTeleporter.calculateEnergyCost(player, teleWorld, coords);
77+
IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0);
78+
if (energyContainer == null || energyContainer.extract(energyCost, Action.SIMULATE, AutomationType.MANUAL) < energyCost) {
79+
return;
80+
}
81+
energyExtraction = () -> energyContainer.extract(energyCost, Action.EXECUTE, AutomationType.MANUAL);
82+
} else {
83+
energyCost = 0L;
84+
}
85+
//TODO: Figure out what this try catch is meant to be catching as I don't see much of a reason for it to exist
86+
try {
87+
teleporter.didTeleport.add(player.getUUID());
88+
teleporter.teleDelay = 5;
89+
BlockPos teleporterTargetPos = teleporter.getTeleporterTargetPos();
90+
MekanismTeleportEvent.PortableTeleporter event = new MekanismTeleportEvent.PortableTeleporter(player, teleporterTargetPos, coords.dimension(), stack, energyCost);
91+
if (NeoForge.EVENT_BUS.post(event).isCanceled()) {
92+
//Fail if the event was cancelled
93+
return;
94+
}
95+
if (energyExtraction != null) {
96+
energyExtraction.run();
97+
}
98+
player.connection.aboveGroundTickCount = 0;
99+
player.closeContainer();
100+
PacketUtils.sendToAllTracking(new PacketPortalFX(player.blockPosition()), player.level(), coords.pos());
101+
if (player.isPassenger()) {
102+
player.stopRiding();
103+
}
104+
double oldX = player.getX();
105+
double oldY = player.getY();
106+
double oldZ = player.getZ();
107+
Level oldWorld = player.level();
108+
TileEntityTeleporter.teleportEntityTo(player, teleWorld, teleporter, event, false, DimensionTransition.DO_NOTHING);
109+
if (player.level() != oldWorld || player.distanceToSqr(oldX, oldY, oldZ) >= 25) {
110+
//If the player teleported over 5 blocks, play the sound at both the destination and the source
111+
oldWorld.playSound(null, oldX, oldY, oldZ, SoundEvents.PLAYER_TELEPORT, SoundSource.PLAYERS);
112+
}
113+
player.level().playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.PLAYER_TELEPORT, SoundSource.PLAYERS);
114+
teleporter.sendTeleportParticles();
115+
} catch (Exception ignored) {
116+
}
117+
}
118+
}
119+
}
120+
});
121+
}
122+
123+
private static Frequency.FrequencyIdentity getIdentity(ItemPortableTeleporter teleporter, ItemStack stack) {
124+
DataComponentType<? extends FrequencyAware<?>> frequencyComponent = MekanismDataComponents.getFrequencyComponent(teleporter.getFrequencyType());
125+
if (frequencyComponent == null) {
126+
return null;
127+
}
128+
FrequencyAware<?> frequencyAware = stack.get(frequencyComponent);
129+
if (frequencyAware != null) {
130+
Frequency.FrequencyIdentity identity = frequencyAware.identity().orElse(null);
131+
if (identity != null) {
132+
return identity;
133+
}
134+
}
135+
136+
return null;
137+
}
138+
}

src/main/java/dev/tonimatas/mekanismcurios/util/KeyBinding.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ public class KeyBinding {
1010
public static final String KEY_CATEGORY = "key.categories.mekanismcurios";
1111
public static final String KEY_PORTABLE_QIO = "key.mekanismcurios.protableqio";
1212
public static final String KEY_PORTABLE_TELEPORTER = "key.mekanismcurios.portableteleporter";
13+
public static final String KEY_QUICK_TELEPORT = "key.mekanismcurios.quickteleport";
1314

1415
public static final Lazy<KeyMapping> PORTABLE_QIO_MAPPING = Lazy.of(() -> new KeyMapping(KEY_PORTABLE_QIO,
1516
KeyConflictContext.IN_GAME, InputConstants.Type.KEYSYM, GLFW.GLFW_KEY_Y, KEY_CATEGORY));
1617
public static final Lazy<KeyMapping> PORTABLE_TELEPORTER_MAPPING = Lazy.of(() -> new KeyMapping(KEY_PORTABLE_TELEPORTER,
1718
KeyConflictContext.IN_GAME, InputConstants.Type.KEYSYM, GLFW.GLFW_KEY_U, KEY_CATEGORY));
19+
public static final Lazy<KeyMapping> QUICK_TELEPORT_MAPPING = Lazy.of(() -> new KeyMapping(KEY_QUICK_TELEPORT,
20+
KeyConflictContext.IN_GAME, InputConstants.Type.KEYSYM, GLFW.GLFW_KEY_I, KEY_CATEGORY));
1821
}

src/main/resources/assets/mekanismcurios/lang/en_us.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"key.categories.mekanismcurios": "Mekanism Curios",
33
"key.mekanismcurios.protableqio": "Open Portable QIO",
44
"key.mekanismcurios.portableteleporter": "Open Portable Teleporter",
5+
"key.mekanismcurios.quickteleport": "Quick-Teleport",
56
"curios.identifier.qio": "QIO",
67
"curios.identifier.teleporter": "Teleporter"
78
}

0 commit comments

Comments
 (0)