Add back command

This commit is contained in:
zontreck 2023-12-19 10:47:00 -07:00
parent a027393534
commit 731a46c241
7 changed files with 94 additions and 3 deletions

View file

@ -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

View file

@ -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)

View file

@ -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.";
}
}

View file

@ -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());
}
}

View file

@ -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<CommandSourceStack> 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;
}
}

View file

@ -46,8 +46,13 @@ public class TeleportActioner
for(int i = 0; i < effects.size(); i++) {
RegistryObject<MobEffect> 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);

View file

@ -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<UUID, WorldPosition> 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");
}
}