Fix the block restore system. Begin final implementation of zschem

This commit is contained in:
Tara 2023-01-07 21:27:49 -07:00
parent b4c73493d4
commit cccb000add
10 changed files with 229 additions and 13 deletions

View file

@ -27,6 +27,9 @@ import dev.zontreck.otemod.commands.warps.RTPWarpCommand;
import dev.zontreck.otemod.commands.warps.SetWarpCommand;
import dev.zontreck.otemod.commands.warps.WarpCommand;
import dev.zontreck.otemod.commands.warps.WarpsCommand;
import dev.zontreck.otemod.commands.zschem.SaveSchem;
import dev.zontreck.otemod.commands.zschem.SetPos1;
import dev.zontreck.otemod.commands.zschem.SetPos2;
import dev.zontreck.otemod.configs.OTEServerConfig;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
@ -121,6 +124,10 @@ public class CommandRegistry {
RTPWarpCommand.register(ev.getDispatcher());
WarpsCommand.register(ev.getDispatcher());
WarpCommand.register(ev.getDispatcher());
SetPos1.register(ev.getDispatcher());
SetPos2.register(ev.getDispatcher());
SaveSchem.register(ev.getDispatcher());
}

View file

@ -20,7 +20,6 @@ import net.minecraft.commands.Commands;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;

View file

@ -3,7 +3,6 @@ package dev.zontreck.otemod.commands.warps;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mojang.brigadier.CommandDispatcher;
@ -13,7 +12,6 @@ import dev.zontreck.libzontreck.chat.HoverTip;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.chat.ChatServerOverride;
import dev.zontreck.otemod.configs.Profile;
import dev.zontreck.otemod.database.TeleportDestination;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.ClickEvent;
@ -21,8 +19,6 @@ import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.Style;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3;
public class WarpsCommand {

View file

@ -1,5 +1,74 @@
package dev.zontreck.otemod.commands.zschem;
import java.util.ArrayList;
import java.util.List;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import dev.zontreck.libzontreck.chat.ChatColor;
import dev.zontreck.libzontreck.vectors.Vector3;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.chat.ChatServerOverride;
import dev.zontreck.otemod.zschem.MemoryHolder;
import dev.zontreck.otemod.zschem.MemoryHolder.Container;
import dev.zontreck.otemod.zschem.StoredBlock;
import net.minecraft.client.gui.components.ChatComponent;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
public class SaveSchem {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("savezschem").executes(c-> saveSchematicUsage(c.getSource())).then(Commands.argument("name", StringArgumentType.string()).executes(z->saveSchematic(z.getSource(), StringArgumentType.getString(z, "name")))));
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
//String arg = StringArgumentType.getString(command, "nickname");
//return setHome(command.getSource(), arg);
//}));
}
private static int saveSchematic(CommandSourceStack source, String name) {
// Perform sanity checks
if(!source.isPlayer())return 1;
ServerPlayer play = source.getPlayer();
if(play==null)return 1;
if(MemoryHolder.hasPlayerCached(play))
{
Container cont = MemoryHolder.get(play);
if(cont == null)
{
saveSchematicUsage(source);
}else {
if(cont.Pos1 != null && cont.Pos2 != null)
{
// Lets go!
List<StoredBlock> blocks = new ArrayList<StoredBlock>();
// First we calculate every vector between pos1 and pos2.
// Then we subtract pos1 from the vector to acquire a relative position.
// Then we save the block with this relative position to the blocks list
// Once serialized, it is then possible to add the position. Note that this makes it impossible to rotate a zschem like with worldedit, but thats usually fine for our usecases. once in-game worldedit can be used to rotate.
// TODO: Also- It is possible that a rotational implementation can be added in the future
}
}
}
ChatServerOverride.broadcastTo(play.getUUID(), Component.literal(ChatColor.doColors("!Dark_Red! You must first set the positions")), OTEMod.THE_SERVER);
return 0;
}
private static int saveSchematicUsage(CommandSourceStack source)
{
String usage = OTEMod.OTEPrefix;
usage += ChatColor.doColors("!gold! /savezschem [string:name]");
ChatServerOverride.broadcastTo(source.getPlayer().getUUID(), Component.literal(usage), OTEMod.THE_SERVER);
return 0;
}
}

View file

@ -1,5 +1,27 @@
package dev.zontreck.otemod.commands.zschem;
import com.mojang.brigadier.CommandDispatcher;
import dev.zontreck.libzontreck.vectors.Vector3;
import dev.zontreck.otemod.zschem.MemoryHolder;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
public class SetPos1 {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("zpos1").executes(c-> setzPos1(c.getSource())));
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
//String arg = StringArgumentType.getString(command, "nickname");
//return setHome(command.getSource(), arg);
//}));
}
private static int setzPos1(CommandSourceStack source) {
MemoryHolder.setPos1(source.getPlayer(), new Vector3(source.getPosition()));
return 0;
}
}

View file

@ -1,5 +1,27 @@
package dev.zontreck.otemod.commands.zschem;
import com.mojang.brigadier.CommandDispatcher;
import dev.zontreck.libzontreck.vectors.Vector3;
import dev.zontreck.otemod.zschem.MemoryHolder;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
public class SetPos2 {
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("zpos2").executes(c-> setzPos2(c.getSource())));
//dispatcher.register(Commands.literal("sethome").then(Commands.argument("nickname", StringArgumentType.string())).executes(command -> {
//String arg = StringArgumentType.getString(command, "nickname");
//return setHome(command.getSource(), arg);
//}));
}
private static int setzPos2(CommandSourceStack source) {
MemoryHolder.setPos2(source.getPlayer(), new Vector3(source.getPosition()));
return 0;
}
}

View file

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@ -12,6 +13,8 @@ import dev.zontreck.otemod.configs.OTEServerConfig;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.level.block.state.BlockState;
public class BlockContainerList {
@ -81,6 +84,9 @@ public class BlockContainerList {
HealRunner.scheduleHeal(storedBlock);
isb.remove();
wp.getActualDimension().playSound(null, wp.Position.asBlockPos(), SoundEvents.ANVIL_USE, SoundSource.NEUTRAL, new Random().nextFloat(0.75f,1.0f), new Random().nextFloat(1));
}else {
HealRunner.scheduleHeal(storedBlock);
storedBlock.setTick(getNewLongestTick());

View file

@ -47,8 +47,11 @@ public class HealRunner implements Runnable
if(be!=null){
//be.deserializeNBT(sb.getBlockEntity());
be.load(BlockToSet.getBlockEntity());
be.setChanged();
if(BlockToSet.getBlockEntity()!=null){
be.load(BlockToSet.getBlockEntity());
be.setChanged();
}
}

View file

@ -0,0 +1,96 @@
package dev.zontreck.otemod.zschem;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import dev.zontreck.libzontreck.vectors.Vector3;
import net.minecraft.world.entity.player.Player;
public class MemoryHolder {
// This class holds the temporary memory related to zschem data
// We also store position information here.
public class Container{
// Contains the position and block lists!
public Vector3 Pos1;
public Vector3 Pos2;
public List<StoredBlock> blocks;
}
private static Map<UUID, Container> playerContainers = new HashMap<UUID,Container>();
private static final Lock lck = new ReentrantLock();
public static boolean hasPlayerCached(Player P)
{
lck.lock();
try{
UUID id = P.getUUID();
return playerContainers.containsKey(id);
}finally{
lck.unlock();
}
}
public static void setPos1(Player p, Vector3 pos)
{
if(hasPlayerCached(p))
{
Container c = playerContainers.get(p.getUUID());
c.Pos1 = pos;
playerContainers.put(p.getUUID(), c);
}
}
public static void clear(Player p)
{
if(hasPlayerCached(p))
{
playerContainers.remove(p.getUUID());
}
}
public static void setPos2(Player p, Vector3 pos)
{
if(hasPlayerCached(p))
{
Container c = playerContainers.get(p.getUUID());
c.Pos2 = pos;
playerContainers.put(p.getUUID(), c);
}
}
public static void setBlocks(Player p, List<StoredBlock> blk)
{
if(hasPlayerCached(p))
{
Container c = playerContainers.get(p.getUUID());
c.blocks=blk;
playerContainers.put(p.getUUID(), c);
}
}
public static List<StoredBlock> getBlocks(Player p)
{
if(hasPlayerCached(p))
{
return playerContainers.get(p.getUUID()).blocks;
}else return new ArrayList<StoredBlock>();
}
public static Container get(Player p)
{
if(hasPlayerCached(p)){
return playerContainers.get(p.getUUID());
}else return null;
}
}

View file

@ -6,9 +6,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import dev.zontreck.libzontreck.vectors.Vector3;
import dev.zontreck.libzontreck.vectors.WorldPosition;
import dev.zontreck.otemod.configs.OTEServerConfig;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
@ -80,8 +78,7 @@ public class WorldProp implements Supplier<Object>
for (Map.Entry<ServerLevel, WorldProp> entry : props.entrySet()) {
// Perform saving
WorldPosition wp = new WorldPosition(new Vector3(), entry.getKey());
String dimsafe = wp.DimSafe;
String dimsafe = WorldPosition.getDimSafe(entry.getKey());
String pathTemp = destBase.toString()+"_"+dimsafe+ext;
Path finalPath = Path.of(pathTemp);
@ -110,8 +107,7 @@ public class WorldProp implements Supplier<Object>
}
Path destBase = BlockSaver.getPath();
String ext = BlockSaver.getExtension();
WorldPosition wp = new WorldPosition(new Vector3(), w);
String dimsafe = wp.DimSafe;
String dimsafe = WorldPosition.getDimSafe(w);
String pathTemp = destBase.toString()+"_"+dimsafe+ext;
Path finalPath = Path.of(pathTemp);