Skip to content

Commit

Permalink
Revert "Use processing for wrecks (#686)"
Browse files Browse the repository at this point in the history
This reverts commit 9ccfe24.
  • Loading branch information
TylerS1066 authored Aug 20, 2024
1 parent 5246f5f commit 1d14370
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 339 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import net.countercraft.movecraft.features.contacts.ContactsCommand;
import net.countercraft.movecraft.features.contacts.ContactsManager;
import net.countercraft.movecraft.features.contacts.ContactsSign;
import net.countercraft.movecraft.features.fading.WreckManager;
import net.countercraft.movecraft.features.status.StatusManager;
import net.countercraft.movecraft.features.status.StatusSign;
import net.countercraft.movecraft.listener.*;
Expand Down Expand Up @@ -58,7 +57,6 @@ public class Movecraft extends JavaPlugin {
private WorldHandler worldHandler;
private SmoothTeleport smoothTeleport;
private AsyncManager asyncManager;
private WreckManager wreckManager;

public static synchronized Movecraft getInstance() {
return instance;
Expand Down Expand Up @@ -191,10 +189,8 @@ public void onEnable() {
asyncManager.runTaskTimer(this, 0, 1);
MapUpdateManager.getInstance().runTaskTimer(this, 0, 1);


CraftManager.initialize(datapackInitialized);
Bukkit.getScheduler().runTaskTimer(this, WorldManager.INSTANCE::run, 0,1);
wreckManager = new WreckManager(WorldManager.INSTANCE);

getServer().getPluginManager().registerEvents(new InteractListener(), this);

Expand Down Expand Up @@ -336,8 +332,4 @@ public SmoothTeleport getSmoothTeleport() {
public AsyncManager getAsyncManager() {
return asyncManager;
}

public @NotNull WreckManager getWreckManager(){
return wreckManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,24 @@
import net.countercraft.movecraft.MovecraftLocation;
import net.countercraft.movecraft.async.rotation.RotationTask;
import net.countercraft.movecraft.async.translation.TranslationTask;
import net.countercraft.movecraft.craft.Craft;
import net.countercraft.movecraft.craft.CraftManager;
import net.countercraft.movecraft.craft.PilotedCraft;
import net.countercraft.movecraft.craft.PlayerCraft;
import net.countercraft.movecraft.craft.SinkingCraft;
import net.countercraft.movecraft.config.Settings;
import net.countercraft.movecraft.craft.*;
import net.countercraft.movecraft.craft.type.CraftType;
import net.countercraft.movecraft.events.CraftReleaseEvent;
import net.countercraft.movecraft.mapUpdater.MapUpdateManager;
import net.countercraft.movecraft.mapUpdater.update.BlockCreateCommand;
import net.countercraft.movecraft.mapUpdater.update.UpdateCommand;
import net.countercraft.movecraft.util.hitboxes.HitBox;
import net.kyori.adventure.text.Component;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

Expand All @@ -51,8 +49,14 @@ public class AsyncManager extends BukkitRunnable {
private final Map<AsyncTask, Craft> ownershipMap = new HashMap<>();
private final BlockingQueue<AsyncTask> finishedAlgorithms = new LinkedBlockingQueue<>();
private final Set<Craft> clearanceSet = new HashSet<>();
private final Map<HitBox, Long> wrecks = new HashMap<>();
private final Map<HitBox, World> wreckWorlds = new HashMap<>();
private final Map<HitBox, Map<Location, BlockData>> wreckPhases = new HashMap<>();
private final Map<World, Set<MovecraftLocation>> processedFadeLocs = new HashMap<>();
private final Map<Craft, Integer> cooldownCache = new WeakHashMap<>();

private long lastFadeCheck = 0;

public AsyncManager() {}

public void submitTask(AsyncTask task, Craft c) {
Expand All @@ -67,6 +71,15 @@ public void submitCompletedTask(AsyncTask task) {
finishedAlgorithms.add(task);
}

public void addWreck(Craft craft){
if(craft.getCollapsedHitBox().isEmpty() || Settings.FadeWrecksAfter == 0){
return;
}
wrecks.put(craft.getCollapsedHitBox(), System.currentTimeMillis());
wreckWorlds.put(craft.getCollapsedHitBox(), craft.getWorld());
wreckPhases.put(craft.getCollapsedHitBox(), craft.getPhaseBlocks());
}

private void processAlgorithmQueue() {
int runLength = 10;
int queueLength = finishedAlgorithms.size();
Expand Down Expand Up @@ -312,11 +325,68 @@ private void processSinking() {
}
}

private void processFadingBlocks() {
if (Settings.FadeWrecksAfter == 0)
return;
long ticksElapsed = (System.currentTimeMillis() - lastFadeCheck) / 50;
if (ticksElapsed <= Settings.FadeTickCooldown)
return;

List<HitBox> processed = new ArrayList<>();
for(Map.Entry<HitBox, Long> entry : wrecks.entrySet()){
if (Settings.FadeWrecksAfter * 1000L > System.currentTimeMillis() - entry.getValue())
continue;

final HitBox hitBox = entry.getKey();
final Map<Location, BlockData> phaseBlocks = wreckPhases.get(hitBox);
final World world = wreckWorlds.get(hitBox);
List<UpdateCommand> commands = new ArrayList<>();
int fadedBlocks = 0;
if (!processedFadeLocs.containsKey(world))
processedFadeLocs.put(world, new HashSet<>());

int maxFadeBlocks = (int) (hitBox.size() * (Settings.FadePercentageOfWreckPerCycle / 100.0));
//Iterate hitbox as a set to get more random locations
for (MovecraftLocation location : hitBox.asSet()){
if (processedFadeLocs.get(world).contains(location))
continue;

if (fadedBlocks >= maxFadeBlocks)
break;

final Location bLoc = location.toBukkit(world);
if ((Settings.FadeWrecksAfter
+ Settings.ExtraFadeTimePerBlock.getOrDefault(bLoc.getBlock().getType(), 0))
* 1000L > System.currentTimeMillis() - entry.getValue())
continue;

fadedBlocks++;
processedFadeLocs.get(world).add(location);
BlockData phaseBlock = phaseBlocks.getOrDefault(bLoc, Material.AIR.createBlockData());
commands.add(new BlockCreateCommand(world, location, phaseBlock));
}
MapUpdateManager.getInstance().scheduleUpdates(commands);
if (!processedFadeLocs.get(world).containsAll(hitBox.asSet()))
continue;

processed.add(hitBox);
processedFadeLocs.get(world).removeAll(hitBox.asSet());
}
for(HitBox hitBox : processed) {
wrecks.remove(hitBox);
wreckPhases.remove(hitBox);
wreckWorlds.remove(hitBox);
}

lastFadeCheck = System.currentTimeMillis();
}

public void run() {
clearAll();

processCruise();
processSinking();
processFadingBlocks();
processAlgorithmQueue();

// now cleanup craft that are bugged and have not moved in the past 60 seconds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public void release(@NotNull Craft craft, @NotNull CraftReleaseEvent.Reason reas
craft.getHitBox().getMinZ())
);
}
Movecraft.getInstance().getWreckManager().queueWreck(craft);
Movecraft.getInstance().getAsyncManager().addWreck(craft);
}

//region Craft management
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 1d14370

Please sign in to comment.