Modulise map crumble

This commit is contained in:
Sam 2018-01-04 12:39:39 +00:00 committed by Alexander Meech
parent 7338f20d81
commit 266d86e3a1
2 changed files with 184 additions and 82 deletions

View File

@ -616,78 +616,6 @@ public abstract class Skywars extends Game
}
}
@EventHandler
public void onGameStateChangeCreateChests(GameStateChangeEvent event)
{
if (event.GetState() != Game.GameState.Live)
{
return;
}
HashSet<Material> ignore = new HashSet<Material>();
ignore.add(Material.LEAVES);
int xDiff = WorldData.MaxX - WorldData.MinX;
int zDiff = WorldData.MaxZ - WorldData.MinZ;
int done = 0;
int attempts = 0;
while (done < GetPlayers(true).size() && attempts <= 1000)
{
attempts++;
Block block = UtilBlock.getHighest(WorldData.World, WorldData.MinX + UtilMath.r(xDiff), WorldData.MinZ + UtilMath.r(zDiff), ignore);
// Dont spawn near player islands
boolean nearPlayer = false;
for (Location loc : GetTeamList().get(0).GetSpawns())
{
if (UtilMath.offset2d(loc, block.getLocation()) < 12)
{
nearPlayer = true;
break;
}
}
if (nearPlayer)
{
continue;
}
// Find LOWEST valid point - prevents spawning on rooftops/etc
Block validChest = null;
while (block.getY() > WorldData.MinY)
{
// Valid Spot for Chest
if ((UtilBlock.airFoliage(block)) && (UtilBlock.solid(block.getRelative(BlockFace.DOWN))))
{
validChest = block;
}
block = block.getRelative(BlockFace.DOWN);
}
if (validChest != null)
{
validChest.setTypeIdAndData(Material.CHEST.getId(), (byte) UtilMath.r(4), false);
_worldBlocks.add(validChest);
done++;
}
}
if (attempts >= 1000)
{
System.out.println("Placed: " + done);
System.out.println("ERROR PLACING RANDOM CHESTS");
System.out.println("X Min:" + WorldData.MinX);
System.out.println("X Diff:" + xDiff);
System.out.println("Z Min:" + WorldData.MinZ);
System.out.println("Z Diff:" + zDiff);
}
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event)
{
@ -1370,16 +1298,7 @@ public abstract class Skywars extends Game
{
_oreHider.Explosion(event);
Iterator<Block> blocks = event.GetBlocks().iterator();
while (blocks.hasNext())
{
Block block = blocks.next();
if (block.getType() == Material.CHEST || block.getType() == Material.ANVIL)
{
blocks.remove();
}
}
event.GetBlocks().removeIf(block -> block.getType() == Material.CHEST || block.getType() == Material.ANVIL);
}
@EventHandler

View File

@ -0,0 +1,183 @@
package nautilus.game.arcade.game.modules;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.scheduler.BukkitRunnable;
import mineplex.core.common.util.MapUtil;
import mineplex.core.common.util.UtilMath;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import nautilus.game.arcade.world.WorldData;
public class MapCrumbleModule extends Module
{
private final Set<Block> _worldBlocks;
private boolean _enabled;
private int _rate = 4;
public MapCrumbleModule()
{
_worldBlocks = new HashSet<>(100000);
}
@Override
protected void setup()
{
WorldData worldData = getGame().WorldData;
getGame().getArcadeManager().runSyncTimer(new BukkitRunnable()
{
int y = worldData.MinY;
@Override
public void run()
{
World world = worldData.World;
for (int x = worldData.MinX; x < worldData.MaxX; x++)
{
for (int z = worldData.MinZ; z < worldData.MaxZ; z++)
{
Block block = world.getBlockAt(x, y, z);
if (block.getType() != Material.AIR && !block.isLiquid())
{
_worldBlocks.add(block);
}
}
}
Bukkit.broadcastMessage("Blocks = " + _worldBlocks.size());
if (++y == worldData.MaxY)
{
cancel();
Bukkit.broadcastMessage("Done");
}
}
}, 1, 1);
}
public MapCrumbleModule setEnabled(boolean enabled)
{
_enabled = enabled;
return this;
}
public MapCrumbleModule setRate(int rate)
{
_rate = rate;
return this;
}
public MapCrumbleModule addWorldBlock(Block block)
{
_worldBlocks.add(block);
return this;
}
@EventHandler
public void crumble(UpdateEvent event)
{
if (event.getType() != UpdateType.TICK || !getGame().IsLive() || _worldBlocks.isEmpty() || !_enabled)
{
return;
}
Location center = getGame().GetSpectatorLocation();
for (int i = 0; i < _rate; i++)
{
Block bestBlock = null;
double bestDist = 0.0D;
for (Block block : _worldBlocks)
{
double dist = UtilMath.offset2dSquared(center, block.getLocation().add(0.5, 0, 0.5));
if (bestBlock == null || dist > bestDist)
{
bestBlock = block;
bestDist = dist;
}
}
if (bestBlock == null)
{
continue;
}
while (bestBlock.getRelative(BlockFace.DOWN).getType() != Material.AIR)
{
bestBlock = bestBlock.getRelative(BlockFace.DOWN);
}
_worldBlocks.remove(bestBlock);
if (!bestBlock.getWorld().equals(center.getWorld()) || bestBlock.getType() == Material.AIR)
{
continue;
}
if (bestBlock.getType() == Material.WOODEN_DOOR || bestBlock.getType() == Material.IRON_DOOR_BLOCK)
{
MapUtil.QuickChangeBlockAt(bestBlock.getRelative(BlockFace.UP).getLocation(), Material.AIR);
}
else if (Math.random() > 0.95)
{
bestBlock.getWorld().spawnFallingBlock(bestBlock.getLocation().add(0.5, 0.5, 0.5), bestBlock.getType(), bestBlock.getData());
}
MapUtil.QuickChangeBlockAt(bestBlock.getLocation(), Material.AIR);
}
}
@EventHandler(priority = EventPriority.MONITOR)
public void blockPlace(BlockPlaceEvent event)
{
if (event.isCancelled())
{
return;
}
_worldBlocks.add(event.getBlock());
}
@EventHandler(priority = EventPriority.MONITOR)
public void blockPlace(BlockBreakEvent event)
{
if (event.isCancelled())
{
return;
}
_worldBlocks.remove(event.getBlock());
}
@EventHandler(priority = EventPriority.MONITOR)
public void blockPhysics(BlockPhysicsEvent event)
{
if (event.isCancelled())
{
return;
}
_worldBlocks.add(event.getBlock());
}
}