Skip to content

Instantly share code, notes, and snippets.

@vemacs
Created September 3, 2015 01:52
Show Gist options
  • Select an option

  • Save vemacs/596fdfbea4e8e8bece31 to your computer and use it in GitHub Desktop.

Select an option

Save vemacs/596fdfbea4e8e8bece31 to your computer and use it in GitHub Desktop.
/*
* Decompiled with CFR 0_101.
*
* Could not load the following classes:
* com.sk89q.worldedit.EditSession
* com.sk89q.worldedit.MaxChangedBlocksException
* com.sk89q.worldedit.Vector
* com.sk89q.worldedit.blocks.BaseBlock
* org.bukkit.plugin.Plugin
* org.bukkit.scheduler.BukkitRunnable
* org.bukkit.scheduler.BukkitTask
*/
package com.mooglemods.wickedskywars.build;
import com.mooglemods.wickedskywars.WickedSkyWars;
import com.mooglemods.wickedskywars.utilities.ChunkInfo;
import com.mooglemods.wickedskywars.utilities.dhutil.NMSHandler;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.*;
public class BlockBuilder
extends BukkitRunnable {
private EditSession editSession;
private Deque<BlockBuilderEntry> vectorQueue;
private Deque<BlockBuilderEntry> delayedQueue;
private int blocksPerTick;
private BuildFinishedHandler buildFinishedHandler;
private Iterable<Vector> cage;
private World w;
private static NMSHandler nmsHandler = new NMSHandler();
private Set<ChunkInfo> toRefresh = new HashSet<>();
public BlockBuilder(EditSession editSession, List<BlockBuilderEntry> vectorQueue, List<BlockBuilderEntry> delayedQueue, int blocksPerTick, Iterable<Vector> cage,
BuildFinishedHandler buildFinishedHandler) {
this.editSession = editSession;
this.vectorQueue = new ArrayDeque<>(vectorQueue);
this.delayedQueue = new ArrayDeque<>(delayedQueue);
this.blocksPerTick = blocksPerTick;
this.buildFinishedHandler = buildFinishedHandler;
this.cage = cage;
this.w = ((BukkitWorld) editSession.getWorld()).getWorld();
WickedSkyWars.get().getLogger().info("Vector size " + vectorQueue.size());
WickedSkyWars.get().getLogger().info("Delay size " + delayedQueue.size());
}
public void start(long delay, long period) {
this.runTaskTimer(WickedSkyWars.get(), delay, period);
}
@Override
public void run() {
try {
long init = System.currentTimeMillis();
long curr = 0;
int i = 0;
while (curr - init < 15) {
curr = System.currentTimeMillis();
i++;
if (!this.vectorQueue.isEmpty()) {
this.placeFast(this.vectorQueue.poll());
continue;
}
if (!this.delayedQueue.isEmpty()) {
this.placeSlow(this.delayedQueue.poll());
continue;
}
this.cancel();
Runnable r = new Runnable() {
@Override
public void run() {
for (ChunkInfo ci : toRefresh) {
w.refreshChunk(ci.x, ci.z);
}
}
};
Bukkit.getScheduler().runTaskLater(WickedSkyWars.get(), r, 1);
Bukkit.getScheduler().runTaskLater(WickedSkyWars.get(), r, 20);
this.buildFinishedHandler.onBuildFinish();
break;
}
WickedSkyWars.get().getLogger().info("Did " + i + " blocks in 15ms");
for (Vector v : cage) {
placeSlow(new BlockBuilderEntry(v, new BaseBlock(Material.GLASS.getId())));
}
}
catch (MaxChangedBlocksException ex) {
this.cancel();
this.buildFinishedHandler.onBuildFinish();
}
}
private void placeFast(BlockBuilderEntry entry) {
Vector v = entry.getLocation();
BaseBlock b = entry.getBlock();
ChunkInfo ci = new ChunkInfo(w, v.getBlockX() >> 4, v.getBlockZ() >> 4);
if (!nmsHandler.setBlockFast(w, v.getBlockX(), v.getBlockY(), v.getBlockZ(), b.getId(), (byte) b.getData())) {
try {
placeSlow(entry);
} catch (MaxChangedBlocksException e) {
e.printStackTrace();
}
}
toRefresh.add(ci);
int x = ci.x;
int z = ci.z;
toRefresh.add(new ChunkInfo(w, x + 1, z + 1));
toRefresh.add(new ChunkInfo(w, x + 1, z - 1));
toRefresh.add(new ChunkInfo(w, x - 1, z + 1));
toRefresh.add(new ChunkInfo(w, x - 1, z - 1));
}
private void placeSlow(BlockBuilderEntry entry) throws MaxChangedBlocksException {
this.editSession.smartSetBlock(entry.getLocation(), entry.getBlock());
}
public interface BuildFinishedHandler {
void onBuildFinish();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment