diff --git a/gradle.properties b/gradle.properties index a883c45..234683a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -49,7 +49,7 @@ mod_name=Aria's Essentials # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=GPLv3 # The mod version. See https://semver.org/ -mod_version=1.1.121823.1856 +mod_version=1.1.121923.1046 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/dev/zontreck/essentials/AriasEssentials.java b/src/main/java/dev/zontreck/essentials/AriasEssentials.java index ce622a0..ce605b4 100644 --- a/src/main/java/dev/zontreck/essentials/AriasEssentials.java +++ b/src/main/java/dev/zontreck/essentials/AriasEssentials.java @@ -19,8 +19,14 @@ import dev.zontreck.essentials.networking.ModMessages; import dev.zontreck.essentials.networking.S2CUpdateHearts; import dev.zontreck.essentials.rtp.RTPCachesEventHandlers; import dev.zontreck.essentials.rtp.RandomPositionFactory; +import dev.zontreck.essentials.util.BackPositionCaches; import dev.zontreck.libzontreck.events.RegisterPacketsEvent; +import dev.zontreck.libzontreck.vectors.WorldPosition; import net.minecraft.server.level.ServerLevel; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.entity.player.Player; +import net.minecraftforge.event.entity.living.LivingDeathEvent; +import net.minecraftforge.eventbus.api.EventPriority; import net.minecraftforge.fml.config.ModConfig; import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import org.slf4j.Logger; @@ -105,6 +111,17 @@ public class AriasEssentials { ALIVE=false; } + @SubscribeEvent (priority = EventPriority.HIGHEST) + public void onPlayerDied(final LivingDeathEvent ev) + { + if(ev.getEntity() instanceof ServerPlayer sp) + { + // Update player back position! + WorldPosition wp = new WorldPosition(sp); + BackPositionCaches.Update(sp.getUUID(), wp); + } + } + // You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent @Mod.EventBusSubscriber(modid = AriasEssentials.MODID, bus = Mod.EventBusSubscriber.Bus.MOD) diff --git a/src/main/java/dev/zontreck/essentials/Messages.java b/src/main/java/dev/zontreck/essentials/Messages.java index 4e8f96e..96660a5 100644 --- a/src/main/java/dev/zontreck/essentials/Messages.java +++ b/src/main/java/dev/zontreck/essentials/Messages.java @@ -70,6 +70,8 @@ public class Messages { public static final String HEARTS_UPDATED; public static final String RESPAWNING; public static final String RTP_CACHED; + public static final String NO_BACK; + public static final String TELEPORT_BACK; static{ @@ -149,5 +151,9 @@ public class Messages { RESPAWNING = ESSENTIALS_PREFIX + "!Dark_Green!Respawning at World Spawn"; RTP_CACHED = ESSENTIALS_PREFIX + "!Dark_Green!A new RTP location has been cached for [0]"; + + NO_BACK = ESSENTIALS_PREFIX + "!Dark_Green!Good news! You have not died recently, there is no saved back position"; + TELEPORT_BACK = ESSENTIALS_PREFIX + "!Dark_Purple!You are being taken back to your last death location now."; + } } diff --git a/src/main/java/dev/zontreck/essentials/commands/CommandRegister.java b/src/main/java/dev/zontreck/essentials/commands/CommandRegister.java index d2bbe98..c5fc37a 100644 --- a/src/main/java/dev/zontreck/essentials/commands/CommandRegister.java +++ b/src/main/java/dev/zontreck/essentials/commands/CommandRegister.java @@ -13,6 +13,7 @@ import dev.zontreck.essentials.commands.warps.WarpCommand; import dev.zontreck.essentials.commands.warps.WarpsCommand; import dev.zontreck.essentials.warps.Warps; import net.minecraftforge.event.RegisterCommandsEvent; +import net.minecraftforge.eventbus.api.EventPriority; import net.minecraftforge.eventbus.api.SubscribeEvent; public class CommandRegister { @@ -39,5 +40,6 @@ public class CommandRegister { HeartsCommand.register(ev.getDispatcher()); SpawnCommand.register(ev.getDispatcher()); + BackCommand.register(ev.getDispatcher()); } } diff --git a/src/main/java/dev/zontreck/essentials/commands/teleport/BackCommand.java b/src/main/java/dev/zontreck/essentials/commands/teleport/BackCommand.java new file mode 100644 index 0000000..02df6e2 --- /dev/null +++ b/src/main/java/dev/zontreck/essentials/commands/teleport/BackCommand.java @@ -0,0 +1,34 @@ +package dev.zontreck.essentials.commands.teleport; + +import com.mojang.brigadier.CommandDispatcher; +import dev.zontreck.essentials.Messages; +import dev.zontreck.essentials.util.BackPositionCaches; +import dev.zontreck.libzontreck.util.ChatHelpers; +import dev.zontreck.libzontreck.vectors.WorldPosition; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.commands.Commands; + +public class BackCommand +{ + public static void register(CommandDispatcher dispatcher) + { + dispatcher.register(Commands.literal("back").executes(c->back(c.getSource()))); + } + + public static int back(CommandSourceStack ctx) + { + try { + WorldPosition wp = BackPositionCaches.Pop(ctx.getPlayer().getUUID()); + + ChatHelpers.broadcastTo(ctx.getPlayer(), ChatHelpers.macro(Messages.TELEPORT_BACK), ctx.getServer()); + + TeleportContainer cont = new TeleportContainer(ctx.getPlayer(), wp.Position.asMinecraftVector(), ctx.getRotation(), wp.getActualDimension()); + + TeleportActioner.ApplyTeleportEffect(ctx.getPlayer()); + TeleportActioner.PerformTeleport(cont); + } catch (Exception e) { + ChatHelpers.broadcastTo(ctx.getPlayer(), ChatHelpers.macro(Messages.NO_BACK), ctx.getServer()); + } + return 0; + } +} diff --git a/src/main/java/dev/zontreck/essentials/commands/teleport/TeleportActioner.java b/src/main/java/dev/zontreck/essentials/commands/teleport/TeleportActioner.java index 4096e7c..b8c59bf 100644 --- a/src/main/java/dev/zontreck/essentials/commands/teleport/TeleportActioner.java +++ b/src/main/java/dev/zontreck/essentials/commands/teleport/TeleportActioner.java @@ -46,8 +46,13 @@ public class TeleportActioner for(int i = 0; i < effects.size(); i++) { RegistryObject effect = RegistryObject.create(new ResourceLocation(effects.get(i)), ForgeRegistries.MOB_EFFECTS); - int duration = AriasEssentials.random.nextInt(5, 15) * 20; - int amplifier = AriasEssentials.random.nextInt(1, 4); + int duration = AriasEssentials.random.nextInt(5, 10) * 20; + int amplifier = AriasEssentials.random.nextInt(1, 3); + + if (effects.get(i).equals("minecraft:slow_falling")) + { + duration = duration*2; + } MobEffectInstance inst = new MobEffectInstance(effect.get(), duration, amplifier, true, true); diff --git a/src/main/java/dev/zontreck/essentials/util/BackPositionCaches.java b/src/main/java/dev/zontreck/essentials/util/BackPositionCaches.java new file mode 100644 index 0000000..0083b38 --- /dev/null +++ b/src/main/java/dev/zontreck/essentials/util/BackPositionCaches.java @@ -0,0 +1,27 @@ +package dev.zontreck.essentials.util; + + +import dev.zontreck.libzontreck.vectors.WorldPosition; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +public class BackPositionCaches +{ + private static final Map backCaches = new HashMap<>(); + public static void Update(UUID ID, WorldPosition pos) + { + backCaches.put(ID, pos); + } + + public static WorldPosition Pop(UUID ID) throws Exception { + if(backCaches.containsKey(ID)) { + WorldPosition pos = backCaches.get(ID); + backCaches.remove(ID); + return pos; + }else throw new Exception("No such back cache"); + } + +}