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.Collection;
import java.util.LinkedList; import java.util.LinkedList;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.configs.OTEServerConfig; import dev.zontreck.otemod.configs.OTEServerConfig;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity; 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.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; 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.ChunkDataEvent;
import net.minecraftforge.event.level.ExplosionEvent; import net.minecraftforge.event.level.ExplosionEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent; 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 public class Handler
{ {
private static final String EXPLOSION_HEAL_TAG = "OTEEH"; private static final String EXPLOSION_HEAL_TAG = "OTEEH";
@ -50,10 +59,13 @@ public class Handler
final Collection<StoredBlock> affectedBlocks = buildBlocks(level, event.getAffectedBlocks()); final Collection<StoredBlock> affectedBlocks = buildBlocks(level, event.getAffectedBlocks());
final Collection<StoredBlock> toHeal = new ArrayList<StoredBlock>(affectedBlocks.size()); final Collection<StoredBlock> toHeal = new ArrayList<StoredBlock>(affectedBlocks.size());
Block tnt = Blocks.TNT;
for(final StoredBlock data : affectedBlocks) for(final StoredBlock data : affectedBlocks)
{ {
// Check an exclusions list // Check an exclusions list
if(OTEServerConfig.EXCLUDE_DIMENSIONS.get().contains(data.getWorldPosition().Dimension)) if(!OTEServerConfig.EXCLUDE_DIMENSIONS.get().contains(data.getWorldPosition().Dimension))
if(!data.getState().isAir() && !data.getState().is(tnt))
toHeal.add(data); toHeal.add(data);
} }
@ -77,7 +89,7 @@ public class Handler
if(data.getBlockEntity()!=null) if(data.getBlockEntity()!=null)
data.getWorldPosition().getActualDimension().removeBlockEntity(data.getPos()); 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 // Add to the healing queue

View file

@ -1,11 +1,13 @@
package dev.zontreck.otemod.antigrief; package dev.zontreck.otemod.antigrief;
import java.io.IOException; import java.io.IOException;
import java.util.Random;
import dev.zontreck.otemod.OTEMod; import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.configs.OTEServerConfig; import dev.zontreck.otemod.configs.OTEServerConfig;
import dev.zontreck.otemod.containers.Vector3; import dev.zontreck.otemod.containers.Vector3;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvent; import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource; import net.minecraft.sounds.SoundSource;
@ -23,7 +25,7 @@ public class HealerManager implements Runnable
// Run the queue // Run the queue
// We want to restore one block per run, then halt for number of seconds in config // We want to restore one block per run, then halt for number of seconds in config
try { try {
Thread.sleep(Long.parseLong(String.valueOf(OTEServerConfig.HEALER_TIMER.get()*1000))); Thread.sleep(OTEServerConfig.HEALER_TIMER.get());
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
e.printStackTrace(); e.printStackTrace();
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -38,24 +40,82 @@ public class HealerManager implements Runnable
} }
// Loop back to start if no items in queue // 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 // 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 // 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 // Remove the block from the queue now to prevent further issues
HealerQueue.ToHeal.remove(sb); HealerQueue.ToHeal.remove(sb);
ServerLevel level = sb.getWorldPosition().getActualDimension(); HealerQueue.ToValidate.add(sb);
level.setBlock(sb.getPos(), sb.getState(), 0); // Healer object should have been added to the validation list
level.getServer().execute(new Runnable(){
public void run()
{
level.setBlockAndUpdate(sb.getPos(), sb.getState());
BlockEntity be = level.getBlockEntity(sb.getPos()); BlockEntity be = level.getBlockEntity(sb.getPos());
if(be!=null)
be.deserializeNBT(sb.getBlockEntity()); be.deserializeNBT(sb.getBlockEntity());
// Everything is restored, play sound // Everything is restored, play sound
SoundSource ss = SoundSource.BLOCKS; SoundSource ss = SoundSource.NEUTRAL;
Vector3 v3 = sb.getWorldPosition().Position; Vector3 v3 = sb.getWorldPosition().Position;
level.playSound(null, v3.x, v3.y, v3.z, pop, ss, 0, 0); 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..."); 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 com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.zontreck.otemod.OTEMod;
import dev.zontreck.otemod.configs.OTEServerConfig; import dev.zontreck.otemod.configs.OTEServerConfig;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag; import net.minecraft.nbt.ListTag;
@ -29,20 +30,23 @@ public class HealerQueue {
public static final String HealerQueueDebugFile = "OTEHealerLastQueue.snbt"; public static final String HealerQueueDebugFile = "OTEHealerLastQueue.snbt";
public static List<StoredBlock> ToHeal = new ArrayList<StoredBlock>(); public static List<StoredBlock> ToHeal = new ArrayList<StoredBlock>();
public static List<StoredBlock> ToValidate = new ArrayList<StoredBlock>();
public static Path getPath() public static Path getPath()
{ {
Path configDir = FMLPaths.GAMEDIR.get().resolve(FMLConfig.defaultConfigPath()); Path configDir = FMLPaths.GAMEDIR.get().resolve(FMLConfig.defaultConfigPath());
Path configFile = null;
if(OTEServerConfig.DEBUG_HEALER.get()) if(OTEServerConfig.DEBUG_HEALER.get())
{ {
Path configFile = configDir.resolve(HealerQueue.HealerQueueDebugFile); configFile = configDir.resolve(HealerQueue.HealerQueueDebugFile);
return configFile;
}else { }else {
Path configFile = configDir.resolve(HealerQueue.HealerQueueFile); configFile = configDir.resolve(HealerQueue.HealerQueueFile);
return configFile;
} }
OTEMod.LOGGER.info("OTE HEALER TEMPORARY FILE: "+configFile.toFile().getAbsolutePath());
return configFile;
} }
public static void Initialize() public static void Initialize()
@ -101,8 +105,14 @@ public class HealerQueue {
{ {
lst.add(block.serialize()); lst.add(block.serialize());
} }
ListTag lst2 = new ListTag();
for(final StoredBlock block : HealerQueue.ToValidate)
{
lst.add(block.serialize());
}
tag.put("queue", lst); tag.put("queue", lst);
tag.put("validate", lst2);
// OK // OK
@ -125,6 +135,14 @@ public class HealerQueue {
StoredBlock sb = new StoredBlock(stored); StoredBlock sb = new StoredBlock(stored);
HealerQueue.ToHeal.add(sb); 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 public static void dump() throws IOException

View file

@ -47,7 +47,7 @@ public class OTEServerConfig {
BUILDER.pop(); BUILDER.pop();
BUILDER.push("ANTIGRIEF").comment("AntiGrief Explosion Healing Events"); 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); 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); 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() public Vector3 moveUp()
{ {
Vector3 up = Clone(); Vector3 up = Clone();