Make more changes to healing code

This commit is contained in:
Zontreck 2022-10-17 02:44:13 -07:00
parent 6820c00034
commit 3b6caf1a03
5 changed files with 126 additions and 21 deletions

View file

@ -4,20 +4,29 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.configs.OTEServerConfig;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.BlockEventData;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.event.entity.item.ItemEvent;
import net.minecraftforge.event.level.BlockEvent;
import net.minecraftforge.event.level.ChunkDataEvent;
import net.minecraftforge.event.level.ExplosionEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
@EventBusSubscriber(modid = OTEMod.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class Handler
{
private static final String EXPLOSION_HEAL_TAG = "OTEEH";
@ -50,11 +59,14 @@ public class Handler
final Collection<StoredBlock> affectedBlocks = buildBlocks(level, event.getAffectedBlocks());
final Collection<StoredBlock> toHeal = new ArrayList<StoredBlock>(affectedBlocks.size());
Block tnt = Blocks.TNT;
for(final StoredBlock data : affectedBlocks)
{
// Check an exclusions list
if(OTEServerConfig.EXCLUDE_DIMENSIONS.get().contains(data.getWorldPosition().Dimension))
toHeal.add(data);
if(!OTEServerConfig.EXCLUDE_DIMENSIONS.get().contains(data.getWorldPosition().Dimension))
if(!data.getState().isAir() && !data.getState().is(tnt))
toHeal.add(data);
}
// Process Block Entities
@ -77,7 +89,7 @@ public class Handler
if(data.getBlockEntity()!=null)
data.getWorldPosition().getActualDimension().removeBlockEntity(data.getPos());
data.getWorldPosition().getActualDimension().removeBlock(data.getPos(), false); // Is false to not drop item?
data.getWorldPosition().getActualDimension().destroyBlock(data.getPos(), false);
}
// Add to the healing queue

View file

@ -1,11 +1,13 @@
package dev.zontreck.otemod.antigrief;
import java.io.IOException;
import java.util.Random;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.configs.OTEServerConfig;
import dev.zontreck.otemod.containers.Vector3;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
@ -23,7 +25,7 @@ public class HealerManager implements Runnable
// Run the queue
// We want to restore one block per run, then halt for number of seconds in config
try {
Thread.sleep(Long.parseLong(String.valueOf(OTEServerConfig.HEALER_TIMER.get()*1000)));
Thread.sleep(OTEServerConfig.HEALER_TIMER.get());
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (InterruptedException e) {
@ -38,24 +40,82 @@ public class HealerManager implements Runnable
}
// Loop back to start if no items in queue
if(HealerQueue.ToHeal.size()==0)continue;
if(HealerQueue.ToHeal.size()==0){
if(HealerQueue.ToValidate.size()==0)continue;
// Validate success
for(StoredBlock sb : HealerQueue.ToValidate)
{
final ServerLevel level = sb.getWorldPosition().getActualDimension();
if(!level.getBlockState(sb.getPos()).is(sb.getState().getBlock()))
{
// Redo restore
HealerQueue.ToHeal.addAll(HealerQueue.ToValidate);
HealerQueue.ToValidate.clear();
break;
}
}
HealerQueue.ToValidate.clear();
continue;
}
// Play a popping sound at the block position
SoundEvent pop = SoundEvents.ITEM_PICKUP;
final SoundEvent pop = SoundEvents.ITEM_PICKUP;
// Get the first block in the list
StoredBlock sb = HealerQueue.ToHeal.get(0);
final StoredBlock sb = HealerQueue.ToHeal.get(0);
final ServerLevel level = sb.getWorldPosition().getActualDimension();
// Remove the block from the queue now to prevent further issues
HealerQueue.ToHeal.remove(sb);
ServerLevel level = sb.getWorldPosition().getActualDimension();
level.setBlock(sb.getPos(), sb.getState(), 0);
BlockEntity be = level.getBlockEntity(sb.getPos());
be.deserializeNBT(sb.getBlockEntity());
HealerQueue.ToValidate.add(sb);
// Healer object should have been added to the validation list
// Everything is restored, play sound
SoundSource ss = SoundSource.BLOCKS;
Vector3 v3 = sb.getWorldPosition().Position;
level.playSound(null, v3.x, v3.y, v3.z, pop, ss, 0, 0);
level.getServer().execute(new Runnable(){
public void run()
{
level.setBlockAndUpdate(sb.getPos(), sb.getState());
BlockEntity be = level.getBlockEntity(sb.getPos());
if(be!=null)
be.deserializeNBT(sb.getBlockEntity());
// Everything is restored, play sound
SoundSource ss = SoundSource.NEUTRAL;
Vector3 v3 = sb.getWorldPosition().Position;
Random rng = new Random();
level.playSound(null, v3.asBlockPos(), pop, ss, rng.nextFloat(0.75f,1.0f), rng.nextFloat(1));
/*for(ServerPlayer player : level.players())
{
Vector3 playerPos = new Vector3(player.position());
if(sb.getWorldPosition().Position.distance(playerPos) < 15)
{
// have player's client play sound (Packet?)
}
}*/
}
});
if(OTEServerConfig.DEBUG_HEALER.get())
try {
HealerQueue.dump();
} catch (IOException e) {
e.printStackTrace();
}
HealerQueue.Shuffle();
}
OTEMod.LOGGER.info("Tearing down healer jobs. Saving remaining queue, stand by...");

View file

@ -14,6 +14,7 @@ import java.util.List;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.configs.OTEServerConfig;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
@ -29,20 +30,23 @@ public class HealerQueue {
public static final String HealerQueueDebugFile = "OTEHealerLastQueue.snbt";
public static List<StoredBlock> ToHeal = new ArrayList<StoredBlock>();
public static List<StoredBlock> ToValidate = new ArrayList<StoredBlock>();
public static Path getPath()
{
Path configDir = FMLPaths.GAMEDIR.get().resolve(FMLConfig.defaultConfigPath());
Path configFile = null;
if(OTEServerConfig.DEBUG_HEALER.get())
{
Path configFile = configDir.resolve(HealerQueue.HealerQueueDebugFile);
return configFile;
configFile = configDir.resolve(HealerQueue.HealerQueueDebugFile);
}else {
Path configFile = configDir.resolve(HealerQueue.HealerQueueFile);
return configFile;
configFile = configDir.resolve(HealerQueue.HealerQueueFile);
}
OTEMod.LOGGER.info("OTE HEALER TEMPORARY FILE: "+configFile.toFile().getAbsolutePath());
return configFile;
}
public static void Initialize()
@ -101,8 +105,14 @@ public class HealerQueue {
{
lst.add(block.serialize());
}
ListTag lst2 = new ListTag();
for(final StoredBlock block : HealerQueue.ToValidate)
{
lst.add(block.serialize());
}
tag.put("queue", lst);
tag.put("validate", lst2);
// OK
@ -125,6 +135,14 @@ public class HealerQueue {
StoredBlock sb = new StoredBlock(stored);
HealerQueue.ToHeal.add(sb);
}
ListTag items2 = tag.getList("validate", Tag.TAG_COMPOUND);
for(int i=0;i<items2.size();i++)
{
CompoundTag stored = items2.getCompound(i);
StoredBlock sb = new StoredBlock(stored);
HealerQueue.ToValidate.add(sb);
}
}
}
public static void dump() throws IOException

View file

@ -47,7 +47,7 @@ public class OTEServerConfig {
BUILDER.pop();
BUILDER.push("ANTIGRIEF").comment("AntiGrief Explosion Healing Events");
HEALER_TIMER = BUILDER.comment("Time between healing events (In Seconds)").define("timer", 5); // Should this be lower?
HEALER_TIMER = BUILDER.comment("Time between healing events (In Milliseconds)").define("timer", 5000); // Should this be lower?
EXCLUDE_DIMENSIONS = BUILDER.comment("What dimensions to exclude? (Namespace:Dimension) in lowercase").define("exclude_dims", defaultExcludeDimensions);
DEBUG_HEALER = BUILDER.comment("Whether or not to debug the healer engine. (Saves as SNBT instead of NBT)").define("debug", false);

View file

@ -71,6 +71,21 @@ public class Vector3
}
}
public Vector3 subtract(Vector3 other)
{
return new Vector3(x-other.x, y-other.y, z-other.z);
}
public Vector3 add(Vector3 other)
{
return new Vector3(x+other.x, y+other.y, z +other.z);
}
public double distance(Vector3 other)
{
Vector3 sub = subtract(other);
return Math.sqrt((sub.x * sub.x + sub.y * sub.y + sub.z * sub.z));
}
public Vector3 moveUp()
{
Vector3 up = Clone();