mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2025-06-26 02:34:42 +02:00
Updated Gradle
This commit is contained in:
@ -0,0 +1,8 @@
|
||||
package com.plotsquared.sponge.util;
|
||||
|
||||
public class KillRoadMobs {
|
||||
public void run() {
|
||||
// TODO kill road mobs
|
||||
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package com.plotsquared.sponge.util;
|
||||
|
||||
import org.spongepowered.api.text.Text;
|
||||
import org.spongepowered.api.text.action.TextActions;
|
||||
import org.spongepowered.api.text.serializer.TextSerializers;
|
||||
|
||||
import com.intellectualcrafters.plot.object.ConsolePlayer;
|
||||
import com.intellectualcrafters.plot.object.PlotMessage;
|
||||
import com.intellectualcrafters.plot.object.PlotPlayer;
|
||||
import com.intellectualcrafters.plot.util.ChatManager;
|
||||
import com.plotsquared.sponge.object.SpongePlayer;
|
||||
|
||||
public class SpongeChatManager extends ChatManager<Text.Builder> {
|
||||
|
||||
@Override
|
||||
public Text.Builder builder() {
|
||||
return Text.builder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void color(final PlotMessage m, final String color) {
|
||||
m.$(this).color(TextSerializers.LEGACY_FORMATTING_CODE.deserialize(color).getColor());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tooltip(final PlotMessage m, final PlotMessage... tooltips) {
|
||||
final Text.Builder builder = Text.builder();
|
||||
boolean lb = false;
|
||||
for (final PlotMessage tooltip : tooltips) {
|
||||
if (lb) {
|
||||
builder.append(Text.of("\n"));
|
||||
}
|
||||
builder.append(tooltip.$(this).build());
|
||||
lb = true;
|
||||
}
|
||||
// AchievementBuilder builder = SpongeMain.THIS.getGame().getRegistry().createAchievementBuilder();
|
||||
m.$(this).onHover(TextActions.showText(builder.toText()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void command(final PlotMessage m, final String command) {
|
||||
m.$(this).onClick(TextActions.runCommand(command));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void text(final PlotMessage m, final String text) {
|
||||
m.$(this).append(Text.of(text));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(final PlotMessage m, final PlotPlayer player) {
|
||||
if (ConsolePlayer.isConsole(player)) {
|
||||
player.sendMessage(m.$(this).build().toPlain());
|
||||
} else {
|
||||
((SpongePlayer) player).player.sendMessage(m.$(this).build());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void suggest(final PlotMessage m, final String command) {
|
||||
m.$(this).onClick(TextActions.suggestCommand(command));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,137 @@
|
||||
package com.plotsquared.sponge.util;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.spongepowered.api.entity.Entity;
|
||||
import org.spongepowered.api.entity.living.Living;
|
||||
import org.spongepowered.api.entity.living.animal.Animal;
|
||||
import org.spongepowered.api.entity.living.monster.Monster;
|
||||
import org.spongepowered.api.world.Chunk;
|
||||
import org.spongepowered.api.world.World;
|
||||
|
||||
import com.intellectualcrafters.plot.object.ChunkLoc;
|
||||
import com.intellectualcrafters.plot.object.Location;
|
||||
import com.intellectualcrafters.plot.object.Plot;
|
||||
import com.intellectualcrafters.plot.util.ChunkManager;
|
||||
import com.intellectualcrafters.plot.util.TaskManager;
|
||||
|
||||
public class SpongeChunkManager extends ChunkManager {
|
||||
|
||||
@Override
|
||||
public int[] countEntities(final Plot plot) {
|
||||
final Location pos1 = plot.getBottomAbs();
|
||||
final Location pos2 = plot.getTopAbs();
|
||||
|
||||
final String worldname = pos1.getWorld();
|
||||
final World world = SpongeUtil.getWorld(worldname);
|
||||
final int bx = pos1.getX();
|
||||
final int bz = pos1.getZ();
|
||||
final int tx = pos2.getX();
|
||||
final int tz = pos2.getZ();
|
||||
final int[] count = new int[6];
|
||||
world.getEntities(new Predicate<Entity>() {
|
||||
@Override
|
||||
public boolean test(final Entity entity) {
|
||||
final org.spongepowered.api.world.Location loc = entity.getLocation();
|
||||
final int x = loc.getBlockX();
|
||||
if ((x >= bx) && (x <= tx)) {
|
||||
final int z = loc.getBlockZ();
|
||||
if ((z >= bz) && (z <= tz)) {
|
||||
count[0]++;
|
||||
if (entity instanceof Living) {
|
||||
count[3]++;
|
||||
if (entity instanceof Animal) {
|
||||
count[1]++;
|
||||
} else if (entity instanceof Monster) {
|
||||
count[2]++;
|
||||
}
|
||||
} else {
|
||||
count[4]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean loadChunk(final String world, final ChunkLoc loc, final boolean force) {
|
||||
final World worldObj = SpongeUtil.getWorld(world);
|
||||
return worldObj.loadChunk(loc.x << 4, 0, loc.z << 4, force).isPresent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<ChunkLoc> getChunkChunks(final String world) {
|
||||
// TODO save world;
|
||||
return super.getChunkChunks(world);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void regenerateChunk(final String world, final ChunkLoc loc) {
|
||||
final World worldObj = SpongeUtil.getWorld(world);
|
||||
final Optional<Chunk> chunkOpt = worldObj.getChunk(loc.x << 4, 0, loc.z << 4);
|
||||
if (chunkOpt.isPresent()) {
|
||||
Chunk chunk = chunkOpt.get();
|
||||
// TODO FIXME
|
||||
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean copyRegion(final Location pos1, final Location pos2, final Location newPos, final Runnable whenDone) {
|
||||
// TODO copy a region
|
||||
TaskManager.runTask(whenDone);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearAllEntities(final Location pos1, final Location pos2) {
|
||||
final String worldname = pos1.getWorld();
|
||||
final World world = SpongeUtil.getWorld(worldname);
|
||||
final int bx = pos1.getX();
|
||||
final int bz = pos1.getZ();
|
||||
final int tx = pos2.getX();
|
||||
final int tz = pos2.getZ();
|
||||
world.getEntities(new Predicate<Entity>() {
|
||||
@Override
|
||||
public boolean test(final Entity entity) {
|
||||
final org.spongepowered.api.world.Location loc = entity.getLocation();
|
||||
final int x = loc.getBlockX();
|
||||
if ((x >= bx) && (x <= tx)) {
|
||||
final int z = loc.getBlockZ();
|
||||
if ((z >= bz) && (z <= tz)) {
|
||||
entity.remove();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swap(Location bot1, Location top1, Location bot2, Location top2, Runnable whenDone) {
|
||||
// TODO swap region
|
||||
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unloadChunk(String world, ChunkLoc loc, boolean save, boolean safe) {
|
||||
final World worldObj = SpongeUtil.getWorld(world);
|
||||
final Optional<Chunk> chunk = worldObj.getChunk(loc.x << 4, 0, loc.z << 4);
|
||||
if (chunk.isPresent()) {
|
||||
worldObj.unloadChunk(chunk.get());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean regenerateRegion(Location pos1, Location pos2, boolean ignoreAugment, Runnable whenDone) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package com.plotsquared.sponge.util;
|
||||
|
||||
import com.intellectualcrafters.plot.commands.MainCommand;
|
||||
import com.intellectualcrafters.plot.object.ConsolePlayer;
|
||||
import com.intellectualcrafters.plot.object.PlotPlayer;
|
||||
import com.plotsquared.sponge.SpongeMain;
|
||||
import org.spongepowered.api.command.CommandCallable;
|
||||
import org.spongepowered.api.command.CommandException;
|
||||
import org.spongepowered.api.command.CommandResult;
|
||||
import org.spongepowered.api.command.CommandSource;
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
import org.spongepowered.api.text.Text;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public class SpongeCommand implements CommandCallable {
|
||||
|
||||
@Override
|
||||
public CommandResult process(final CommandSource cmd, final String string) throws CommandException {
|
||||
final String id = cmd.getIdentifier();
|
||||
PlotPlayer pp;
|
||||
try {
|
||||
final UUID uuid = UUID.fromString(id);
|
||||
final Player player = SpongeMain.THIS.getServer().getPlayer(uuid).get();
|
||||
pp = SpongeUtil.getPlayer(player);
|
||||
} catch (final Exception e) {
|
||||
pp = ConsolePlayer.getConsole();
|
||||
}
|
||||
if (MainCommand.onCommand(pp, cmd.getName(), string.isEmpty() ? new String[]{} : string.split(" "))) {
|
||||
return CommandResult.success();
|
||||
} else {
|
||||
return CommandResult.empty();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getSuggestions(final CommandSource cmd, final String string) throws CommandException {
|
||||
// TODO Auto-generated method stub
|
||||
return new ArrayList<>(Collections.singletonList("TEST"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean testPermission(final CommandSource cmd) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<? extends Text> getShortDescription(final CommandSource cmd) {
|
||||
return Optional.of(Text.of("Shows plot help"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<? extends Text> getHelp(final CommandSource cmd) {
|
||||
return Optional.of(Text.of("/plot help"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Text getUsage(final CommandSource cmd) {
|
||||
return Text.of("/plot <command>");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
package com.plotsquared.sponge.util;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.event.Listener;
|
||||
import org.spongepowered.api.event.cause.Cause;
|
||||
import org.spongepowered.api.event.service.ChangeServiceProviderEvent;
|
||||
import org.spongepowered.api.service.economy.EconomyService;
|
||||
import org.spongepowered.api.service.economy.account.UniqueAccount;
|
||||
|
||||
import com.intellectualcrafters.plot.object.OfflinePlotPlayer;
|
||||
import com.intellectualcrafters.plot.object.PlotPlayer;
|
||||
import com.intellectualcrafters.plot.util.EconHandler;
|
||||
import com.intellectualcrafters.plot.util.UUIDHandler;
|
||||
import com.plotsquared.sponge.object.SpongePlayer;
|
||||
|
||||
public class SpongeEconHandler extends EconHandler {
|
||||
private EconomyService econ;
|
||||
|
||||
public SpongeEconHandler() {
|
||||
if (Sponge.getServiceManager().isRegistered(EconomyService.class)) {
|
||||
econ = Sponge.getServiceManager().provide(EconomyService.class).get();
|
||||
}
|
||||
}
|
||||
|
||||
@Listener
|
||||
public void onChangeServiceProvider(ChangeServiceProviderEvent event) {
|
||||
if (event.getService().equals(EconomyService.class)) {
|
||||
econ = (EconomyService) event.getNewProviderRegistration().getProvider();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void withdrawMoney(PlotPlayer player, double amount) {
|
||||
if (econ != null) {
|
||||
Optional<UniqueAccount> accOpt = econ.getAccount(player.getUUID());
|
||||
if (accOpt.isPresent()) {
|
||||
UniqueAccount acc = accOpt.get();
|
||||
|
||||
acc.withdraw(econ.getDefaultCurrency(), new BigDecimal(amount), Cause.of("PlotSquared"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void depositMoney(PlotPlayer player, double amount) {
|
||||
if (econ != null) {
|
||||
Optional<UniqueAccount> accOpt = econ.getAccount(player.getUUID());
|
||||
if (accOpt.isPresent()) {
|
||||
UniqueAccount acc = accOpt.get();
|
||||
acc.deposit(econ.getDefaultCurrency(), new BigDecimal(amount), Cause.of("PlotSquared"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void depositMoney(OfflinePlotPlayer player, double amount) {
|
||||
if (econ != null) {
|
||||
Optional<UniqueAccount> accOpt = econ.getAccount(player.getUUID());
|
||||
if (accOpt.isPresent()) {
|
||||
UniqueAccount acc = accOpt.get();
|
||||
acc.deposit(econ.getDefaultCurrency(), new BigDecimal(amount), Cause.of("PlotSquared"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPermission(String world, String player, String perm, boolean value) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("TODO/WIP/NOT IMPLEMENTED!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(String world, String player, String perm) {
|
||||
SpongePlayer obj = (SpongePlayer) UUIDHandler.getPlayer(player);
|
||||
if (obj != null) {
|
||||
return obj.player.hasPermission(perm);
|
||||
}
|
||||
// TODO offline
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getBalance(PlotPlayer player) {
|
||||
if (econ != null) {
|
||||
Optional<UniqueAccount> accOpt = econ.getAccount(player.getUUID());
|
||||
if (accOpt.isPresent()) {
|
||||
UniqueAccount acc = accOpt.get();
|
||||
BigDecimal balance = acc.getBalance(econ.getDefaultCurrency());
|
||||
return balance.doubleValue();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
package com.plotsquared.sponge.util;
|
||||
|
||||
import com.intellectualcrafters.plot.flag.Flag;
|
||||
import com.intellectualcrafters.plot.object.Location;
|
||||
import com.intellectualcrafters.plot.object.Plot;
|
||||
import com.intellectualcrafters.plot.object.PlotArea;
|
||||
import com.intellectualcrafters.plot.object.PlotCluster;
|
||||
import com.intellectualcrafters.plot.object.PlotId;
|
||||
import com.intellectualcrafters.plot.object.PlotPlayer;
|
||||
import com.intellectualcrafters.plot.object.Rating;
|
||||
import com.intellectualcrafters.plot.util.EventUtil;
|
||||
import com.plotsquared.sponge.SpongeMain;
|
||||
import com.plotsquared.sponge.events.ClusterFlagRemoveEvent;
|
||||
import com.plotsquared.sponge.events.PlayerClaimPlotEvent;
|
||||
import com.plotsquared.sponge.events.PlayerEnterPlotEvent;
|
||||
import com.plotsquared.sponge.events.PlayerLeavePlotEvent;
|
||||
import com.plotsquared.sponge.events.PlayerPlotDeniedEvent;
|
||||
import com.plotsquared.sponge.events.PlayerPlotHelperEvent;
|
||||
import com.plotsquared.sponge.events.PlayerPlotTrustedEvent;
|
||||
import com.plotsquared.sponge.events.PlayerTeleportToPlotEvent;
|
||||
import com.plotsquared.sponge.events.PlotClearEvent;
|
||||
import com.plotsquared.sponge.events.PlotDeleteEvent;
|
||||
import com.plotsquared.sponge.events.PlotFlagAddEvent;
|
||||
import com.plotsquared.sponge.events.PlotFlagRemoveEvent;
|
||||
import com.plotsquared.sponge.events.PlotMergeEvent;
|
||||
import com.plotsquared.sponge.events.PlotRateEvent;
|
||||
import com.plotsquared.sponge.events.PlotUnlinkEvent;
|
||||
import org.spongepowered.api.event.Event;
|
||||
import org.spongepowered.api.event.EventManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.UUID;
|
||||
|
||||
public class SpongeEventUtil extends EventUtil {
|
||||
|
||||
public EventManager events;
|
||||
|
||||
public SpongeEventUtil() {
|
||||
events = SpongeMain.THIS.getGame().getEventManager();
|
||||
}
|
||||
|
||||
public boolean callEvent(final Event event) {
|
||||
return !events.post(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean callClaim(final PlotPlayer player, final Plot plot, final boolean auto) {
|
||||
return callEvent(new PlayerClaimPlotEvent(SpongeUtil.getPlayer(player), plot, auto));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean callTeleport(final PlotPlayer player, final Location from, final Plot plot) {
|
||||
return callEvent(new PlayerTeleportToPlotEvent(SpongeUtil.getPlayer(player), from, plot));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean callClear(final Plot plot) {
|
||||
return callEvent(new PlotClearEvent(plot));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void callDelete(Plot plot) {
|
||||
callEvent(new PlotDeleteEvent(plot));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean callFlagAdd(final Flag flag, final Plot plot) {
|
||||
return callEvent(new PlotFlagAddEvent(flag, plot));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean callFlagRemove(final Flag flag, final Plot plot) {
|
||||
return callEvent(new PlotFlagRemoveEvent(flag, plot));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean callMerge(final Plot plot, final ArrayList<PlotId> plots) {
|
||||
return callEvent(new PlotMergeEvent(SpongeUtil.getWorld(plot.getArea().worldname), plot, plots));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean callUnlink(final PlotArea area, final ArrayList<PlotId> plots) {
|
||||
return callEvent(new PlotUnlinkEvent(SpongeUtil.getWorld(area.worldname), plots));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void callEntry(final PlotPlayer player, final Plot plot) {
|
||||
callEvent(new PlayerEnterPlotEvent(SpongeUtil.getPlayer(player), plot));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void callLeave(final PlotPlayer player, final Plot plot) {
|
||||
callEvent(new PlayerLeavePlotEvent(SpongeUtil.getPlayer(player), plot));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void callDenied(final PlotPlayer initiator, final Plot plot, final UUID player, final boolean added) {
|
||||
callEvent(new PlayerPlotDeniedEvent(SpongeUtil.getPlayer(initiator), plot, player, added));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void callTrusted(final PlotPlayer initiator, final Plot plot, final UUID player, final boolean added) {
|
||||
callEvent(new PlayerPlotHelperEvent(SpongeUtil.getPlayer(initiator), plot, player, added));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void callMember(final PlotPlayer initiator, final Plot plot, final UUID player, final boolean added) {
|
||||
callEvent(new PlayerPlotTrustedEvent(SpongeUtil.getPlayer(initiator), plot, player, added));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean callFlagRemove(final Flag flag, final PlotCluster cluster) {
|
||||
return callEvent(new ClusterFlagRemoveEvent(flag, cluster));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rating callRating(final PlotPlayer player, final Plot plot, final Rating rating) {
|
||||
final PlotRateEvent event = new PlotRateEvent(player, rating, plot);
|
||||
events.post(event);
|
||||
return event.getRating();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package com.plotsquared.sponge.util;
|
||||
|
||||
import org.spongepowered.api.block.BlockState;
|
||||
import org.spongepowered.api.block.BlockTypes;
|
||||
import org.spongepowered.api.world.World;
|
||||
|
||||
import com.intellectualcrafters.plot.generator.HybridUtils;
|
||||
import com.intellectualcrafters.plot.object.PlotAnalysis;
|
||||
import com.intellectualcrafters.plot.object.PlotBlock;
|
||||
import com.intellectualcrafters.plot.object.RegionWrapper;
|
||||
import com.intellectualcrafters.plot.object.RunnableVal;
|
||||
|
||||
public class SpongeHybridUtils extends HybridUtils {
|
||||
|
||||
@Override
|
||||
public int checkModified(final String worldname, final int x1, final int x2, final int y1, final int y2, final int z1, final int z2, final PlotBlock[] blocks) {
|
||||
final World world = SpongeUtil.getWorld(worldname);
|
||||
int count = 0;
|
||||
for (int y = y1; y <= y2; y++) {
|
||||
for (int x = x1; x <= x2; x++) {
|
||||
for (int z = z1; z <= z2; z++) {
|
||||
final BlockState state = world.getBlock(x, y, z);
|
||||
final PlotBlock block = SpongeUtil.getPlotBlock(state);
|
||||
boolean same = false;
|
||||
for (final PlotBlock p : blocks) {
|
||||
if (block.id == p.id) {
|
||||
same = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!same) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int get_ey(final String worldname, final int sx, final int ex, final int sz, final int ez, final int sy) {
|
||||
final World world = SpongeUtil.getWorld(worldname);
|
||||
int ey = sy;
|
||||
for (int x = sx; x <= ex; x++) {
|
||||
for (int z = sz; z <= ez; z++) {
|
||||
for (int y = sy; y < 256; y++) {
|
||||
if (y > ey) {
|
||||
final BlockState state = world.getBlock(x, y, z);
|
||||
if ((state != null) && (state.getType() != BlockTypes.AIR)) {
|
||||
ey = y;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void analyzeRegion(String world, RegionWrapper region, RunnableVal<PlotAnalysis> whenDone) {
|
||||
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
package com.plotsquared.sponge.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
import org.spongepowered.api.item.ItemType;
|
||||
import org.spongepowered.api.item.ItemTypes;
|
||||
import org.spongepowered.api.item.inventory.Carrier;
|
||||
import org.spongepowered.api.item.inventory.ItemStack;
|
||||
import org.spongepowered.api.item.inventory.custom.CustomInventory;
|
||||
import org.spongepowered.api.item.inventory.property.SlotIndex;
|
||||
import org.spongepowered.api.item.inventory.type.CarriedInventory;
|
||||
|
||||
import com.intellectualcrafters.plot.object.PlotInventory;
|
||||
import com.intellectualcrafters.plot.object.PlotItemStack;
|
||||
import com.intellectualcrafters.plot.object.PlotPlayer;
|
||||
import com.intellectualcrafters.plot.util.InventoryUtil;
|
||||
import com.plotsquared.sponge.SpongeMain;
|
||||
import com.plotsquared.sponge.object.SpongePlayer;
|
||||
|
||||
public class SpongeInventoryUtil extends InventoryUtil {
|
||||
|
||||
public ItemStack.Builder builder;
|
||||
|
||||
public SpongeInventoryUtil() {
|
||||
builder = SpongeMain.THIS.getGame().getRegistry().createBuilder(ItemStack.Builder.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void open(final PlotInventory inv) {
|
||||
// TODO Auto-generated method stub
|
||||
final SpongePlayer sp = (SpongePlayer) inv.player;
|
||||
final Player player = sp.player;
|
||||
|
||||
final CustomInventory inventory = SpongeMain.THIS.getGame().getRegistry().createBuilder(CustomInventory.Builder.class).name(SpongeUtil.getTranslation(inv.getTitle())).size(inv.size).build();
|
||||
final PlotItemStack[] items = inv.getItems();
|
||||
for (int i = 0; i < (inv.size * 9); i++) {
|
||||
final PlotItemStack item = items[i];
|
||||
if (item != null) {
|
||||
inventory.set(new SlotIndex(i), getItem(item));
|
||||
}
|
||||
}
|
||||
inv.player.setMeta("inventory", inv);
|
||||
player.openInventory(inventory);
|
||||
}
|
||||
|
||||
public ItemStack getItem(final PlotItemStack item) {
|
||||
// FIXME item type, item data, item name, item lore
|
||||
return builder.itemType(ItemTypes.SPONGE).quantity(item.amount).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(final PlotInventory inv) {
|
||||
if (!inv.isOpen()) {
|
||||
return;
|
||||
}
|
||||
inv.player.deleteMeta("inventory");
|
||||
final SpongePlayer sp = (SpongePlayer) inv.player;
|
||||
sp.player.closeInventory();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setItem(final PlotInventory inv, final int index, final PlotItemStack item) {
|
||||
if (!inv.isOpen()) {
|
||||
return;
|
||||
}
|
||||
final SpongePlayer sp = (SpongePlayer) inv.player;
|
||||
final Player player = sp.player;
|
||||
player.getOpenInventory().get();
|
||||
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
|
||||
|
||||
}
|
||||
|
||||
public PlotItemStack getItem(final ItemStack item) {
|
||||
if (item == null) {
|
||||
return null;
|
||||
}
|
||||
final ItemType type = item.getItem();
|
||||
final String id = type.getId();
|
||||
final int amount = item.getQuantity();
|
||||
// TODO name / lore
|
||||
return new PlotItemStack(id, amount, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlotItemStack[] getItems(final PlotPlayer player) {
|
||||
final SpongePlayer sp = (SpongePlayer) player;
|
||||
sp.player.getInventory();
|
||||
new ArrayList<PlotItemStack>();
|
||||
|
||||
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
|
||||
|
||||
// return list.toArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOpen(final PlotInventory inv) {
|
||||
if (!inv.isOpen()) {
|
||||
return false;
|
||||
}
|
||||
final SpongePlayer sp = (SpongePlayer) inv.player;
|
||||
final Player player = sp.player;
|
||||
if (player.isViewingInventory()) {
|
||||
final CarriedInventory<? extends Carrier> inventory = player.getInventory();
|
||||
return inv.getTitle().equals(inventory.getName().getId()); // TODO getId()
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,521 @@
|
||||
package com.plotsquared.sponge.util;
|
||||
|
||||
/*
|
||||
* Copyright 2011-2013 Tyler Blair. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and contributors and should not be interpreted as representing official policies,
|
||||
* either expressed or implied, of anybody else.
|
||||
*/
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.Proxy;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
|
||||
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
|
||||
import ninja.leaping.configurate.loader.ConfigurationLoader;
|
||||
|
||||
import org.spongepowered.api.Game;
|
||||
import org.spongepowered.api.plugin.PluginContainer;
|
||||
import org.spongepowered.api.scheduler.Task;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.intellectualcrafters.plot.PS;
|
||||
|
||||
public class SpongeMetrics {
|
||||
|
||||
/**
|
||||
* The current revision number
|
||||
*/
|
||||
private final static int REVISION = 7;
|
||||
|
||||
/**
|
||||
* The base url of the metrics domain
|
||||
*/
|
||||
private static final String BASE_URL = "http://report.mcstats.org";
|
||||
|
||||
/**
|
||||
* The url used to report a server's status
|
||||
*/
|
||||
private static final String REPORT_URL = "/plugin/%s";
|
||||
|
||||
/**
|
||||
* Interval of time to ping (in minutes)
|
||||
*/
|
||||
private static final int PING_INTERVAL = 15;
|
||||
|
||||
/**
|
||||
* The game data is being sent for
|
||||
*/
|
||||
private final Game game;
|
||||
|
||||
/**
|
||||
* The plugin this metrics submits for
|
||||
*/
|
||||
private final PluginContainer plugin;
|
||||
/**
|
||||
* Lock for synchronization
|
||||
*/
|
||||
private final Object optOutLock = new Object();
|
||||
/**
|
||||
* The plugin configuration file
|
||||
*/
|
||||
private CommentedConfigurationNode config;
|
||||
/**
|
||||
* The configuration loader
|
||||
*/
|
||||
private ConfigurationLoader<CommentedConfigurationNode> configurationLoader;
|
||||
/**
|
||||
* The plugin configuration file
|
||||
*/
|
||||
private File configurationFile;
|
||||
/**
|
||||
* Unique server id
|
||||
*/
|
||||
private String guid;
|
||||
/**
|
||||
* Debug mode
|
||||
*/
|
||||
private boolean debug;
|
||||
/**
|
||||
* The scheduled task
|
||||
*/
|
||||
private volatile Task task = null;
|
||||
|
||||
@Inject
|
||||
public SpongeMetrics(final Game game, final PluginContainer plugin) throws IOException {
|
||||
if (plugin == null) {
|
||||
throw new IllegalArgumentException("Plugin cannot be null");
|
||||
}
|
||||
|
||||
this.game = game;
|
||||
this.plugin = plugin;
|
||||
|
||||
loadConfiguration();
|
||||
}
|
||||
|
||||
/**
|
||||
* GZip compress a string of bytes
|
||||
*
|
||||
* @param input
|
||||
* @return
|
||||
*/
|
||||
public static byte[] gzip(final String input) {
|
||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
GZIPOutputStream gzos = null;
|
||||
|
||||
try {
|
||||
gzos = new GZIPOutputStream(baos);
|
||||
gzos.write(input.getBytes("UTF-8"));
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (gzos != null) {
|
||||
try {
|
||||
gzos.close();
|
||||
} catch (final IOException ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a json encoded key/value pair to the given string builder.
|
||||
*
|
||||
* @param json
|
||||
* @param key
|
||||
* @param value
|
||||
* @throws java.io.UnsupportedEncodingException
|
||||
*/
|
||||
private static void appendJSONPair(final StringBuilder json, final String key, final String value) throws UnsupportedEncodingException {
|
||||
boolean isValueNumeric = false;
|
||||
|
||||
try {
|
||||
if (value.equals("0") || !value.endsWith("0")) {
|
||||
Double.parseDouble(value);
|
||||
isValueNumeric = true;
|
||||
}
|
||||
} catch (final NumberFormatException e) {
|
||||
isValueNumeric = false;
|
||||
}
|
||||
|
||||
if (json.charAt(json.length() - 1) != '{') {
|
||||
json.append(',');
|
||||
}
|
||||
|
||||
json.append(escapeJSON(key));
|
||||
json.append(':');
|
||||
|
||||
if (isValueNumeric) {
|
||||
json.append(value);
|
||||
} else {
|
||||
json.append(escapeJSON(value));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Escape a string to create a valid JSON string
|
||||
*
|
||||
* @param text
|
||||
* @return
|
||||
*/
|
||||
private static String escapeJSON(final String text) {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
|
||||
builder.append('"');
|
||||
for (int index = 0; index < text.length(); index++) {
|
||||
final char chr = text.charAt(index);
|
||||
|
||||
switch (chr) {
|
||||
case '"':
|
||||
case '\\':
|
||||
builder.append('\\');
|
||||
builder.append(chr);
|
||||
break;
|
||||
case '\b':
|
||||
builder.append("\\b");
|
||||
break;
|
||||
case '\t':
|
||||
builder.append("\\t");
|
||||
break;
|
||||
case '\n':
|
||||
builder.append("\\n");
|
||||
break;
|
||||
case '\r':
|
||||
builder.append("\\r");
|
||||
break;
|
||||
default:
|
||||
if (chr < ' ') {
|
||||
final String t = "000" + Integer.toHexString(chr);
|
||||
builder.append("\\u" + t.substring(t.length() - 4));
|
||||
} else {
|
||||
builder.append(chr);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
builder.append('"');
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode text as UTF-8
|
||||
*
|
||||
* @param text the text to encode
|
||||
* @return the encoded text, as UTF-8
|
||||
*/
|
||||
private static String urlEncode(final String text) throws UnsupportedEncodingException {
|
||||
return URLEncoder.encode(text, "UTF-8");
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the configuration
|
||||
*/
|
||||
private void loadConfiguration() {
|
||||
configurationFile = getConfigFile();
|
||||
configurationLoader = HoconConfigurationLoader.builder().setFile(configurationFile).build();
|
||||
|
||||
try {
|
||||
if (!configurationFile.exists()) {
|
||||
configurationFile.createNewFile();
|
||||
config = configurationLoader.load();
|
||||
|
||||
config.setComment("This contains settings for MCStats: http://mcstats.org");
|
||||
config.getNode("mcstats.guid").setValue(UUID.randomUUID().toString());
|
||||
config.getNode("mcstats.opt-out").setValue(false);
|
||||
config.getNode("mcstats.debug").setValue(false);
|
||||
|
||||
configurationLoader.save(config);
|
||||
} else {
|
||||
config = configurationLoader.load();
|
||||
}
|
||||
|
||||
guid = config.getNode("mcstats.guid").getString();
|
||||
debug = config.getNode("mcstats.debug").getBoolean();
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start measuring statistics. This will immediately create an async repeating task as the plugin and send the
|
||||
* initial data to the metrics backend, and then after that it will post in increments of PING_INTERVAL * 1200
|
||||
* ticks.
|
||||
*
|
||||
* @return True if statistics measuring is running, otherwise false.
|
||||
*/
|
||||
public boolean start() {
|
||||
synchronized (optOutLock) {
|
||||
// Did we opt out?
|
||||
if (isOptOut()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Is metrics already running?
|
||||
if (task != null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Begin hitting the server with glorious data
|
||||
final Task.Builder builder = game.getScheduler().createTaskBuilder();
|
||||
builder.async().interval(PING_INTERVAL, TimeUnit.MINUTES).execute(new Runnable() {
|
||||
|
||||
private boolean firstPost = true;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
// This has to be synchronized or it can collide with the disable method.
|
||||
synchronized (optOutLock) {
|
||||
// Disable Task, if it is running and the server owner decided to opt-out
|
||||
if (isOptOut() && (task != null)) {
|
||||
task.cancel();
|
||||
task = null;
|
||||
}
|
||||
}
|
||||
|
||||
// We use the inverse of firstPost because if it is the first time we are posting,
|
||||
// it is not a interval ping, so it evaluates to FALSE
|
||||
// Each time thereafter it will evaluate to TRUE, i.e PING!
|
||||
postPlugin(!firstPost);
|
||||
|
||||
// After the first post we set firstPost to false
|
||||
// Each post thereafter will be a ping
|
||||
firstPost = false;
|
||||
} catch (final IOException e) {
|
||||
if (debug) {
|
||||
PS.debug("[Metrics] " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Has the server owner denied plugin metrics?
|
||||
*
|
||||
* @return true if metrics should be opted out of it
|
||||
*/
|
||||
public boolean isOptOut() {
|
||||
synchronized (optOutLock) {
|
||||
loadConfiguration();
|
||||
|
||||
return config.getNode("mcstats.opt-out").getBoolean();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables metrics for the server by setting "opt-out" to false in the config file and starting the metrics task.
|
||||
*
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public void enable() throws IOException {
|
||||
// This has to be synchronized or it can collide with the check in the task.
|
||||
synchronized (optOutLock) {
|
||||
// Check if the server owner has already set opt-out, if not, set it.
|
||||
if (isOptOut()) {
|
||||
config.getNode("mcstats.opt-out").setValue(false);
|
||||
configurationLoader.save(config);
|
||||
}
|
||||
|
||||
// Enable Task, if it is not running
|
||||
if (task == null) {
|
||||
start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables metrics for the server by setting "opt-out" to true in the config file and canceling the metrics task.
|
||||
*
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public void disable() throws IOException {
|
||||
// This has to be synchronized or it can collide with the check in the task.
|
||||
synchronized (optOutLock) {
|
||||
// Check if the server owner has already set opt-out, if not, set it.
|
||||
if (!isOptOut()) {
|
||||
config.getNode("mcstats.opt-out").setValue(true);
|
||||
configurationLoader.save(config);
|
||||
}
|
||||
|
||||
// Disable Task, if it is running
|
||||
if (task != null) {
|
||||
task.cancel();
|
||||
task = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the File object of the config file that should be used to store data such as the GUID and opt-out status
|
||||
*
|
||||
* @return the File object for the config file
|
||||
*/
|
||||
public File getConfigFile() {
|
||||
// TODO configDir
|
||||
final File configFolder = new File("config");
|
||||
|
||||
return new File(configFolder, "PluginMetrics.conf");
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic method that posts a plugin to the metrics website
|
||||
*
|
||||
*/
|
||||
private void postPlugin(final boolean isPing) throws IOException {
|
||||
// Server software specific section
|
||||
final String pluginName = plugin.getName();
|
||||
final boolean onlineMode = game.getServer().getOnlineMode(); // TRUE if online mode is enabled
|
||||
final String pluginVersion = plugin.getVersion();
|
||||
// TODO no visible way to get MC version at the moment
|
||||
// TODO added by game.getPlatform().getMinecraftVersion() -- impl in 2.1
|
||||
final String serverVersion = String.format("%s %s", "Sponge", game.getPlatform().getMinecraftVersion());
|
||||
final int playersOnline = game.getServer().getOnlinePlayers().size();
|
||||
|
||||
// END server software specific section -- all code below does not use any code outside of this class / Java
|
||||
|
||||
// Construct the post data
|
||||
final StringBuilder json = new StringBuilder(1024);
|
||||
json.append('{');
|
||||
|
||||
// The plugin's description file containg all of the plugin data such as name, version, author, etc
|
||||
appendJSONPair(json, "guid", guid);
|
||||
appendJSONPair(json, "plugin_version", pluginVersion);
|
||||
appendJSONPair(json, "server_version", serverVersion);
|
||||
appendJSONPair(json, "players_online", Integer.toString(playersOnline));
|
||||
|
||||
// New data as of R6
|
||||
final String osname = System.getProperty("os.name");
|
||||
String osarch = System.getProperty("os.arch");
|
||||
final String osversion = System.getProperty("os.version");
|
||||
final String java_version = System.getProperty("java.version");
|
||||
final int coreCount = Runtime.getRuntime().availableProcessors();
|
||||
|
||||
// normalize os arch .. amd64 -> x86_64
|
||||
if (osarch.equals("amd64")) {
|
||||
osarch = "x86_64";
|
||||
}
|
||||
|
||||
appendJSONPair(json, "osname", osname);
|
||||
appendJSONPair(json, "osarch", osarch);
|
||||
appendJSONPair(json, "osversion", osversion);
|
||||
appendJSONPair(json, "cores", Integer.toString(coreCount));
|
||||
appendJSONPair(json, "auth_mode", onlineMode ? "1" : "0");
|
||||
appendJSONPair(json, "java_version", java_version);
|
||||
|
||||
// If we're pinging, append it
|
||||
if (isPing) {
|
||||
appendJSONPair(json, "ping", "1");
|
||||
}
|
||||
|
||||
// close json
|
||||
json.append('}');
|
||||
|
||||
// Create the url
|
||||
final URL url = new URL(BASE_URL + String.format(REPORT_URL, urlEncode(pluginName)));
|
||||
|
||||
// Connect to the website
|
||||
URLConnection connection;
|
||||
|
||||
// Mineshafter creates a socks proxy, so we can safely bypass it
|
||||
// It does not reroute POST requests so we need to go around it
|
||||
if (isMineshafterPresent()) {
|
||||
connection = url.openConnection(Proxy.NO_PROXY);
|
||||
} else {
|
||||
connection = url.openConnection();
|
||||
}
|
||||
|
||||
final byte[] uncompressed = json.toString().getBytes();
|
||||
final byte[] compressed = gzip(json.toString());
|
||||
|
||||
// Headers
|
||||
connection.addRequestProperty("User-Agent", "MCStats/" + REVISION);
|
||||
connection.addRequestProperty("Content-Type", "application/json");
|
||||
connection.addRequestProperty("Content-Encoding", "gzip");
|
||||
connection.addRequestProperty("Content-Length", Integer.toString(compressed.length));
|
||||
connection.addRequestProperty("Accept", "application/json");
|
||||
connection.addRequestProperty("Connection", "close");
|
||||
|
||||
connection.setDoOutput(true);
|
||||
|
||||
if (debug) {
|
||||
PS.debug("[Metrics] Prepared request for " + pluginName + " uncompressed=" + uncompressed.length + " compressed=" + compressed.length);
|
||||
}
|
||||
|
||||
// Write the data
|
||||
final OutputStream os = connection.getOutputStream();
|
||||
os.write(compressed);
|
||||
os.flush();
|
||||
|
||||
// Now read the response
|
||||
final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
String response = reader.readLine();
|
||||
|
||||
// close resources
|
||||
os.close();
|
||||
reader.close();
|
||||
|
||||
if ((response == null) || response.startsWith("ERR") || response.startsWith("7")) {
|
||||
if (response == null) {
|
||||
response = "null";
|
||||
} else if (response.startsWith("7")) {
|
||||
response = response.substring(response.startsWith("7,") ? 2 : 1);
|
||||
}
|
||||
|
||||
throw new IOException(response);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if mineshafter is present. If it is, we need to bypass it to send POST requests
|
||||
*
|
||||
* @return true if mineshafter is installed on the server
|
||||
*/
|
||||
private boolean isMineshafterPresent() {
|
||||
try {
|
||||
Class.forName("mineshafter.MineServer");
|
||||
return true;
|
||||
} catch (final Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,310 @@
|
||||
package com.plotsquared.sponge.util;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import org.spongepowered.api.block.BlockState;
|
||||
import org.spongepowered.api.item.inventory.Carrier;
|
||||
import org.spongepowered.api.item.inventory.type.CarriedInventory;
|
||||
import org.spongepowered.api.world.World;
|
||||
|
||||
import com.intellectualcrafters.jnbt.ByteArrayTag;
|
||||
import com.intellectualcrafters.jnbt.CompoundTag;
|
||||
import com.intellectualcrafters.jnbt.IntTag;
|
||||
import com.intellectualcrafters.jnbt.ListTag;
|
||||
import com.intellectualcrafters.jnbt.ShortTag;
|
||||
import com.intellectualcrafters.jnbt.StringTag;
|
||||
import com.intellectualcrafters.jnbt.Tag;
|
||||
import com.intellectualcrafters.plot.object.ChunkLoc;
|
||||
import com.intellectualcrafters.plot.object.Location;
|
||||
import com.intellectualcrafters.plot.object.PlotBlock;
|
||||
import com.intellectualcrafters.plot.object.RegionWrapper;
|
||||
import com.intellectualcrafters.plot.object.RunnableVal;
|
||||
import com.intellectualcrafters.plot.util.MainUtil;
|
||||
import com.intellectualcrafters.plot.util.SchematicHandler;
|
||||
import com.intellectualcrafters.plot.util.TaskManager;
|
||||
|
||||
public class SpongeSchematicHandler extends SchematicHandler {
|
||||
|
||||
@Override
|
||||
public void getCompoundTag(final String world, final Set<RegionWrapper> regions, final RunnableVal<CompoundTag> whenDone) {
|
||||
// async
|
||||
TaskManager.runTaskAsync(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// Main positions
|
||||
Location[] corners = MainUtil.getCorners(world, regions);
|
||||
final Location bot = corners[0];
|
||||
final Location top = corners[1];
|
||||
|
||||
final int width = (top.getX() - bot.getX()) + 1;
|
||||
final int height = (top.getY() - bot.getY()) + 1;
|
||||
final int length = (top.getZ() - bot.getZ()) + 1;
|
||||
// Main Schematic tag
|
||||
final HashMap<String, Tag> schematic = new HashMap<>();
|
||||
schematic.put("Width", new ShortTag("Width", (short) width));
|
||||
schematic.put("Length", new ShortTag("Length", (short) length));
|
||||
schematic.put("Height", new ShortTag("Height", (short) height));
|
||||
schematic.put("Materials", new StringTag("Materials", "Alpha"));
|
||||
schematic.put("WEOriginX", new IntTag("WEOriginX", 0));
|
||||
schematic.put("WEOriginY", new IntTag("WEOriginY", 0));
|
||||
schematic.put("WEOriginZ", new IntTag("WEOriginZ", 0));
|
||||
schematic.put("WEOffsetX", new IntTag("WEOffsetX", 0));
|
||||
schematic.put("WEOffsetY", new IntTag("WEOffsetY", 0));
|
||||
schematic.put("WEOffsetZ", new IntTag("WEOffsetZ", 0));
|
||||
// Arrays of data types
|
||||
final List<Tag> tileEntities = new ArrayList<Tag>();
|
||||
final byte[] blocks = new byte[width * height * length];
|
||||
final byte[] blockData = new byte[width * height * length];
|
||||
// Queue
|
||||
final ArrayDeque<RegionWrapper> queue = new ArrayDeque<>(regions);
|
||||
TaskManager.runTask(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (queue.isEmpty()) {
|
||||
TaskManager.runTaskAsync(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
schematic.put("Blocks", new ByteArrayTag("Blocks", blocks));
|
||||
schematic.put("Data", new ByteArrayTag("Data", blockData));
|
||||
schematic.put("Entities", new ListTag("Entities", CompoundTag.class, new ArrayList<Tag>()));
|
||||
schematic.put("TileEntities", new ListTag("TileEntities", CompoundTag.class, tileEntities));
|
||||
whenDone.value = new CompoundTag("Schematic", schematic);
|
||||
TaskManager.runTask(whenDone);
|
||||
System.gc();
|
||||
System.gc();
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
final Runnable regionTask = this;
|
||||
RegionWrapper region = queue.poll();
|
||||
Location pos1 = new Location(world, region.minX, region.minY, region.minZ);
|
||||
Location pos2 = new Location(world, region.maxX, region.maxY, region.maxZ);
|
||||
final int bx = bot.getX();
|
||||
final int bz = bot.getZ();
|
||||
final int p1x = pos1.getX();
|
||||
final int p1z = pos1.getZ();
|
||||
final int p2x = pos2.getX();
|
||||
final int p2z = pos2.getZ();
|
||||
final int bcx = p1x >> 4;
|
||||
final int bcz = p1z >> 4;
|
||||
final int tcx = p2x >> 4;
|
||||
final int tcz = p2z >> 4;
|
||||
final int sy = pos1.getY();
|
||||
final int ey = pos2.getY();
|
||||
// Generate list of chunks
|
||||
final ArrayList<ChunkLoc> chunks = new ArrayList<ChunkLoc>();
|
||||
for (int x = bcx; x <= tcx; x++) {
|
||||
for (int z = bcz; z <= tcz; z++) {
|
||||
chunks.add(new ChunkLoc(x, z));
|
||||
}
|
||||
}
|
||||
final World worldObj = SpongeUtil.getWorld(world);
|
||||
// Main thread
|
||||
TaskManager.runTask(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final long start = System.currentTimeMillis();
|
||||
while ((!chunks.isEmpty()) && ((System.currentTimeMillis() - start) < 20)) {
|
||||
// save schematics
|
||||
final ChunkLoc chunk = chunks.remove(0);
|
||||
final int X = chunk.x;
|
||||
final int Z = chunk.z;
|
||||
int xxb = X << 4;
|
||||
int zzb = Z << 4;
|
||||
if (!worldObj.getChunk(xxb, 1, zzb).isPresent() && !worldObj.loadChunk(xxb, 1, zzb, false).isPresent()) {
|
||||
System.out.println("COULD NOT LOAD CHUNK AT: " + chunk.x + "," + chunk.z);
|
||||
continue;
|
||||
}
|
||||
int xxt = xxb + 15;
|
||||
int zzt = zzb + 15;
|
||||
|
||||
if (X == bcx) {
|
||||
xxb = p1x;
|
||||
}
|
||||
if (X == tcx) {
|
||||
xxt = p2x;
|
||||
}
|
||||
if (Z == bcz) {
|
||||
zzb = p1z;
|
||||
}
|
||||
if (Z == tcz) {
|
||||
zzt = p2z;
|
||||
}
|
||||
for (int y = sy; y <= Math.min(255, ey); y++) {
|
||||
final int ry = y - sy;
|
||||
final int i1 = (ry * width * length);
|
||||
for (int z = zzb; z <= zzt; z++) {
|
||||
final int rz = z - p1z;
|
||||
final int i2 = i1 + (rz * width);
|
||||
for (int x = xxb; x <= xxt; x++) {
|
||||
final int rx = x - p1x;
|
||||
final int index = i2 + rx;
|
||||
|
||||
final BlockState state = worldObj.getBlock(x, y, z);
|
||||
PlotBlock block = SpongeUtil.getPlotBlock(state);
|
||||
final int id = block.id;
|
||||
switch (id) {
|
||||
case 0:
|
||||
case 2:
|
||||
case 4:
|
||||
case 13:
|
||||
case 14:
|
||||
case 15:
|
||||
case 20:
|
||||
case 21:
|
||||
case 22:
|
||||
case 24:
|
||||
case 30:
|
||||
case 32:
|
||||
case 37:
|
||||
case 39:
|
||||
case 40:
|
||||
case 41:
|
||||
case 42:
|
||||
case 45:
|
||||
case 46:
|
||||
case 47:
|
||||
case 48:
|
||||
case 49:
|
||||
case 51:
|
||||
case 55:
|
||||
case 56:
|
||||
case 57:
|
||||
case 58:
|
||||
case 60:
|
||||
case 7:
|
||||
case 8:
|
||||
case 9:
|
||||
case 10:
|
||||
case 11:
|
||||
case 73:
|
||||
case 74:
|
||||
case 78:
|
||||
case 79:
|
||||
case 80:
|
||||
case 81:
|
||||
case 82:
|
||||
case 83:
|
||||
case 85:
|
||||
case 87:
|
||||
case 88:
|
||||
case 101:
|
||||
case 102:
|
||||
case 103:
|
||||
case 110:
|
||||
case 112:
|
||||
case 113:
|
||||
case 121:
|
||||
case 122:
|
||||
case 129:
|
||||
case 133:
|
||||
case 165:
|
||||
case 166:
|
||||
case 169:
|
||||
case 170:
|
||||
case 172:
|
||||
case 173:
|
||||
case 174:
|
||||
case 181:
|
||||
case 182:
|
||||
case 188:
|
||||
case 189:
|
||||
case 190:
|
||||
case 191:
|
||||
case 192: {
|
||||
break;
|
||||
}
|
||||
case 54:
|
||||
case 130:
|
||||
case 142:
|
||||
case 27:
|
||||
case 137:
|
||||
case 52:
|
||||
case 154:
|
||||
case 84:
|
||||
case 25:
|
||||
case 144:
|
||||
case 138:
|
||||
case 176:
|
||||
case 177:
|
||||
case 63:
|
||||
case 68:
|
||||
case 323:
|
||||
case 117:
|
||||
case 116:
|
||||
case 28:
|
||||
case 66:
|
||||
case 157:
|
||||
case 61:
|
||||
case 62:
|
||||
case 140:
|
||||
case 146:
|
||||
case 149:
|
||||
case 150:
|
||||
case 158:
|
||||
case 23:
|
||||
case 123:
|
||||
case 124:
|
||||
case 29:
|
||||
case 33:
|
||||
case 151:
|
||||
case 178: {
|
||||
CompoundTag rawTag;
|
||||
if (state instanceof Carrier) {
|
||||
Carrier chest = (Carrier) state;
|
||||
CarriedInventory<? extends Carrier> inv = chest.getInventory();
|
||||
// TODO serialize inventory
|
||||
rawTag = null;
|
||||
} else {
|
||||
rawTag = null;
|
||||
}
|
||||
if (rawTag != null) {
|
||||
final Map<String, Tag> values = new HashMap<String, Tag>();
|
||||
for (final Entry<String, Tag> entry : rawTag.getValue().entrySet()) {
|
||||
values.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
values.put("id", new StringTag("id", "Chest"));
|
||||
values.put("x", new IntTag("x", x));
|
||||
values.put("y", new IntTag("y", y));
|
||||
values.put("z", new IntTag("z", z));
|
||||
final CompoundTag tileEntityTag = new CompoundTag(values);
|
||||
tileEntities.add(tileEntityTag);
|
||||
}
|
||||
}
|
||||
default: {
|
||||
blockData[index] = block.data;
|
||||
}
|
||||
}
|
||||
blocks[index] = (byte) id;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!chunks.isEmpty()) {
|
||||
TaskManager.runTaskLater(this, 1);
|
||||
} else {
|
||||
regionTask.run();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restoreTag(CompoundTag ct, short x, short y, short z, Schematic schem) {
|
||||
// TODO Auto-generated method stub
|
||||
// This method should place the compound tag at a location e.g. chest contents
|
||||
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,176 @@
|
||||
package com.plotsquared.sponge.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.world.DimensionTypes;
|
||||
import org.spongepowered.api.world.GeneratorTypes;
|
||||
import org.spongepowered.api.world.World;
|
||||
import org.spongepowered.api.world.WorldCreationSettings;
|
||||
import org.spongepowered.api.world.WorldCreationSettings.Builder;
|
||||
import org.spongepowered.api.world.gen.WorldGenerator;
|
||||
import org.spongepowered.api.world.gen.WorldGeneratorModifier;
|
||||
import org.spongepowered.api.world.storage.WorldProperties;
|
||||
|
||||
import com.intellectualcrafters.configuration.ConfigurationSection;
|
||||
import com.intellectualcrafters.plot.PS;
|
||||
import com.intellectualcrafters.plot.config.ConfigurationNode;
|
||||
import com.intellectualcrafters.plot.generator.GeneratorWrapper;
|
||||
import com.intellectualcrafters.plot.generator.HybridGen;
|
||||
import com.intellectualcrafters.plot.object.PlotArea;
|
||||
import com.intellectualcrafters.plot.object.SetupObject;
|
||||
import com.intellectualcrafters.plot.util.SetupUtils;
|
||||
import com.plotsquared.sponge.generator.SpongePlotGenerator;
|
||||
|
||||
public class SpongeSetupUtils extends SetupUtils {
|
||||
|
||||
@Override
|
||||
public void updateGenerators() {
|
||||
if (!SetupUtils.generators.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
SetupUtils.generators.put("PlotSquared", new SpongePlotGenerator(new HybridGen()));
|
||||
// TODO get external world generators
|
||||
Collection<WorldGeneratorModifier> wgms = Sponge.getRegistry().getAllOf(WorldGeneratorModifier.class);
|
||||
for (WorldGeneratorModifier wgm : wgms) {
|
||||
String id = wgm.getId();
|
||||
String name = wgm.getName();
|
||||
if (wgm instanceof GeneratorWrapper<?>) {
|
||||
generators.put(id, (GeneratorWrapper<?>) wgm);
|
||||
generators.put(name, (GeneratorWrapper<?>) wgm);
|
||||
} else {
|
||||
SpongePlotGenerator wrap = new SpongePlotGenerator(wgm);
|
||||
generators.put(id, wrap);
|
||||
generators.put(name, wrap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getGenerator(final PlotArea plotworld) {
|
||||
if (SetupUtils.generators.isEmpty()) {
|
||||
updateGenerators();
|
||||
}
|
||||
final World world = SpongeUtil.getWorld(plotworld.worldname);
|
||||
if (world == null) {
|
||||
return null;
|
||||
}
|
||||
final WorldGenerator generator = world.getWorldGenerator();
|
||||
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String setupWorld(final SetupObject object) {
|
||||
SetupUtils.manager.updateGenerators();
|
||||
ConfigurationNode[] steps = object.step == null ? new ConfigurationNode[0] : object.step;
|
||||
final String world = object.world;
|
||||
int type = object.type;
|
||||
String worldPath = "worlds." + object.world;
|
||||
if (!PS.get().config.contains(worldPath)) {
|
||||
PS.get().config.createSection(worldPath);
|
||||
}
|
||||
ConfigurationSection worldSection = PS.get().config.getConfigurationSection(worldPath);
|
||||
switch (type) {
|
||||
case 2: {
|
||||
if (object.id != null) {
|
||||
String areaname = object.id + "-" + object.min + "-" + object.max;
|
||||
String areaPath = "areas." + areaname;
|
||||
if (!worldSection.contains(areaPath)) {
|
||||
worldSection.createSection(areaPath);
|
||||
}
|
||||
ConfigurationSection areaSection = worldSection.getConfigurationSection(areaPath);
|
||||
HashMap<String, Object> options = new HashMap<>();
|
||||
for (final ConfigurationNode step : steps) {
|
||||
options.put(step.getConstant(), step.getValue());
|
||||
}
|
||||
options.put("generator.type", object.type);
|
||||
options.put("generator.terrain", object.terrain);
|
||||
options.put("generator.plugin", object.plotManager);
|
||||
if ((object.setupGenerator != null) && !object.setupGenerator.equals(object.plotManager)) {
|
||||
options.put("generator.init", object.setupGenerator);
|
||||
}
|
||||
for (Entry<String, Object> entry : options.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
Object value = entry.getValue();
|
||||
if (worldSection.contains(key)) {
|
||||
Object current = worldSection.get(key);
|
||||
if (!Objects.equals(value, current)) {
|
||||
areaSection.set(key, value);
|
||||
}
|
||||
} else {
|
||||
worldSection.set(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
GeneratorWrapper<?> gen = generators.get(object.setupGenerator);
|
||||
if ((gen != null) && gen.isFull()) {
|
||||
object.setupGenerator = null;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
for (final ConfigurationNode step : steps) {
|
||||
worldSection.set(step.getConstant(), step.getValue());
|
||||
}
|
||||
PS.get().config.set("worlds." + world + "." + "generator.type", object.type);
|
||||
PS.get().config.set("worlds." + world + "." + "generator.terrain", object.terrain);
|
||||
PS.get().config.set("worlds." + world + "." + "generator.plugin", object.plotManager);
|
||||
if ((object.setupGenerator != null) && !object.setupGenerator.equals(object.plotManager)) {
|
||||
PS.get().config.set("worlds." + world + "." + "generator.init", object.setupGenerator);
|
||||
}
|
||||
GeneratorWrapper<?> gen = generators.get(object.setupGenerator);
|
||||
if ((gen != null) && gen.isFull()) {
|
||||
object.setupGenerator = null;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 0: {
|
||||
for (final ConfigurationNode step : steps) {
|
||||
worldSection.set(step.getConstant(), step.getValue());
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
try {
|
||||
PS.get().config.save(PS.get().configFile);
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (object.setupGenerator != null) {
|
||||
// create world with generator
|
||||
GeneratorWrapper<?> gw = generators.get(object.setupGenerator);
|
||||
WorldGeneratorModifier wgm = (WorldGeneratorModifier) gw.getPlatformGenerator();
|
||||
|
||||
WorldCreationSettings settings = Sponge.getRegistry().createBuilder(Builder.class)
|
||||
.name(object.world)
|
||||
.loadsOnStartup(true)
|
||||
.keepsSpawnLoaded(false)
|
||||
.dimension(DimensionTypes.OVERWORLD)
|
||||
.generator(GeneratorTypes.FLAT)
|
||||
.usesMapFeatures(false)
|
||||
.enabled(true)
|
||||
.generatorModifiers(wgm)
|
||||
.build();
|
||||
WorldProperties properties = Sponge.getServer().createWorldProperties(settings).get();
|
||||
World worldObj = Sponge.getServer().loadWorld(properties).get();
|
||||
} else {
|
||||
// create vanilla world
|
||||
WorldCreationSettings settings = Sponge.getRegistry().createBuilder(Builder.class)
|
||||
.name(object.world)
|
||||
.loadsOnStartup(true)
|
||||
.keepsSpawnLoaded(false)
|
||||
.dimension(DimensionTypes.OVERWORLD)
|
||||
.generator(GeneratorTypes.OVERWORLD)
|
||||
.usesMapFeatures(true)
|
||||
.enabled(true)
|
||||
.build();
|
||||
WorldProperties properties = Sponge.getServer().createWorldProperties(settings).get();
|
||||
World worldObj = Sponge.getServer().loadWorld(properties).get();
|
||||
}
|
||||
return object.world;
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package com.plotsquared.sponge.util;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.spongepowered.api.scheduler.Task;
|
||||
|
||||
import com.intellectualcrafters.plot.util.TaskManager;
|
||||
import com.plotsquared.sponge.SpongeMain;
|
||||
|
||||
public class SpongeTaskManager extends TaskManager {
|
||||
|
||||
private final AtomicInteger i = new AtomicInteger();
|
||||
|
||||
private final HashMap<Integer, Task> tasks = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public int taskRepeat(final Runnable r, final int interval) {
|
||||
final int val = i.incrementAndGet();
|
||||
final Task.Builder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
||||
final Task.Builder built = builder.delayTicks(interval).intervalTicks(interval).execute(r);
|
||||
final Task task = built.submit(SpongeMain.THIS.getPlugin());
|
||||
tasks.put(val, task);
|
||||
return val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int taskRepeatAsync(final Runnable r, final int interval) {
|
||||
final int val = i.incrementAndGet();
|
||||
final Task.Builder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
||||
final Task.Builder built = builder.delayTicks(interval).async().intervalTicks(interval).execute(r);
|
||||
final Task task = built.submit(SpongeMain.THIS.getPlugin());
|
||||
tasks.put(val, task);
|
||||
return val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void taskAsync(final Runnable r) {
|
||||
final Task.Builder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
||||
builder.async().execute(r).submit(SpongeMain.THIS.getPlugin());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void task(final Runnable r) {
|
||||
final Task.Builder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
||||
builder.execute(r).submit(SpongeMain.THIS.getPlugin());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void taskLater(final Runnable r, final int delay) {
|
||||
final Task.Builder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
||||
builder.delayTicks(delay).execute(r).submit(SpongeMain.THIS.getPlugin());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void taskLaterAsync(final Runnable r, final int delay) {
|
||||
final Task.Builder builder = SpongeMain.THIS.getGame().getScheduler().createTaskBuilder();
|
||||
builder.async().delayTicks(delay).execute(r).submit(SpongeMain.THIS.getPlugin());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelTask(final int i) {
|
||||
final Task task = tasks.remove(i);
|
||||
if (task != null) {
|
||||
task.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.plotsquared.sponge.util;
|
||||
|
||||
import com.intellectualcrafters.plot.object.PlotPlayer;
|
||||
import com.intellectualcrafters.plot.util.AbstractTitle;
|
||||
import com.plotsquared.sponge.object.SpongePlayer;
|
||||
import org.spongepowered.api.text.Text;
|
||||
import org.spongepowered.api.text.title.Title;
|
||||
|
||||
public class SpongeTitleManager extends AbstractTitle {
|
||||
|
||||
@Override
|
||||
public void sendTitle(final PlotPlayer player, final String head, final String sub, final int in, final int delay, final int out) {
|
||||
final Title title = Title.builder().title(Text.of(head)).subtitle(Text.of(sub)).fadeIn(in * 20).stay(delay * 20).fadeOut(out * 20).build();
|
||||
((SpongePlayer) player).player.sendTitle(title);
|
||||
}
|
||||
}
|
450
Sponge/src/main/java/com/plotsquared/sponge/util/SpongeUtil.java
Normal file
450
Sponge/src/main/java/com/plotsquared/sponge/util/SpongeUtil.java
Normal file
@ -0,0 +1,450 @@
|
||||
package com.plotsquared.sponge.util;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.block.BlockState;
|
||||
import org.spongepowered.api.block.BlockType;
|
||||
import org.spongepowered.api.block.BlockTypes;
|
||||
import org.spongepowered.api.block.tileentity.Sign;
|
||||
import org.spongepowered.api.block.tileentity.TileEntity;
|
||||
import org.spongepowered.api.data.manipulator.mutable.tileentity.SignData;
|
||||
import org.spongepowered.api.data.value.mutable.ListValue;
|
||||
import org.spongepowered.api.entity.Entity;
|
||||
import org.spongepowered.api.entity.living.player.Player;
|
||||
import org.spongepowered.api.event.cause.Cause;
|
||||
import org.spongepowered.api.text.Text;
|
||||
import org.spongepowered.api.text.translation.Translatable;
|
||||
import org.spongepowered.api.text.translation.Translation;
|
||||
import org.spongepowered.api.world.World;
|
||||
import org.spongepowered.api.world.biome.BiomeType;
|
||||
import org.spongepowered.api.world.biome.BiomeTypes;
|
||||
import org.spongepowered.api.world.extent.Extent;
|
||||
|
||||
import com.flowpowered.math.vector.Vector3d;
|
||||
import com.flowpowered.math.vector.Vector3i;
|
||||
import com.intellectualcrafters.plot.PS;
|
||||
import com.intellectualcrafters.plot.object.Location;
|
||||
import com.intellectualcrafters.plot.object.PlotBlock;
|
||||
import com.intellectualcrafters.plot.object.PlotPlayer;
|
||||
import com.intellectualcrafters.plot.object.RegionWrapper;
|
||||
import com.intellectualcrafters.plot.object.schematic.PlotItem;
|
||||
import com.intellectualcrafters.plot.util.MathMan;
|
||||
import com.intellectualcrafters.plot.util.ReflectionUtils;
|
||||
import com.intellectualcrafters.plot.util.StringComparison;
|
||||
import com.intellectualcrafters.plot.util.StringMan;
|
||||
import com.intellectualcrafters.plot.util.UUIDHandler;
|
||||
import com.intellectualcrafters.plot.util.WorldUtil;
|
||||
import com.plotsquared.sponge.SpongeMain;
|
||||
import com.plotsquared.sponge.object.SpongePlayer;
|
||||
|
||||
public class SpongeUtil extends WorldUtil {
|
||||
|
||||
public static Location getLocation(final Entity player) {
|
||||
final String world = player.getWorld().getName();
|
||||
final org.spongepowered.api.world.Location loc = player.getLocation();
|
||||
final Vector3i pos = loc.getBlockPosition();
|
||||
return new Location(world, pos.getX(), pos.getY(), pos.getZ());
|
||||
}
|
||||
|
||||
private static BiomeType[] biomes;
|
||||
private static HashMap<String, Integer> biomeMap;
|
||||
|
||||
public static BiomeType getBiome(String biome) {
|
||||
if (biomes == null) {
|
||||
initBiomeCache();
|
||||
}
|
||||
return biomes[biomeMap.get(biome.toUpperCase())];
|
||||
}
|
||||
|
||||
public static <T> T getCause(Cause cause, Class<T> clazz) {
|
||||
Optional<?> root = Optional.of(cause.root());
|
||||
if (root.isPresent()) {
|
||||
Object source = root.get();
|
||||
if (clazz.isInstance(source)) {
|
||||
return (T) source;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void printCause(String method, Cause cause) {
|
||||
System.out.println(method + ": " + cause.toString());
|
||||
System.out.println(method + ": " + cause.getClass());
|
||||
System.out.println(method + ": " + StringMan.getString(cause.all()));
|
||||
System.out.println(method + ": " + (cause.root()));
|
||||
}
|
||||
|
||||
public static void initBiomeCache() {
|
||||
try {
|
||||
Field[] fields = BiomeTypes.class.getFields();
|
||||
biomes = new BiomeType[fields.length];
|
||||
biomeMap = new HashMap<>();
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
Field field = fields[i];
|
||||
String name = field.getName();
|
||||
biomeMap.put(name, i);
|
||||
biomes[i] = (BiomeType) field.get(null);
|
||||
}
|
||||
|
||||
} catch (IllegalArgumentException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static BiomeType getBiome(int index) {
|
||||
if (biomes == null) {
|
||||
initBiomeCache();
|
||||
}
|
||||
return biomes[index];
|
||||
}
|
||||
|
||||
public static Text text(String m) {
|
||||
return Text.of(m);
|
||||
}
|
||||
|
||||
public static Translation getTranslation(final String m) {
|
||||
return new Translatable() {
|
||||
@Override
|
||||
public Translation getTranslation() {
|
||||
return new Translation() {
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return m;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String get(final Locale l, final Object... args) {
|
||||
return m;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String get(final Locale l) {
|
||||
return m;
|
||||
}
|
||||
};
|
||||
}
|
||||
}.getTranslation();
|
||||
}
|
||||
|
||||
private static HashMap<BlockState, PlotBlock> stateMap;
|
||||
private static BlockState[] stateArray;
|
||||
|
||||
private static void initBlockCache() {
|
||||
try {
|
||||
PS.debug("Caching block id/data: Please wait...");
|
||||
stateArray = new BlockState[Character.MAX_VALUE];
|
||||
stateMap = new HashMap<>();
|
||||
Method methodGetByCombinedId = ReflectionUtils.findMethod(Class.forName("net.minecraft.block.Block"), true, Class.forName("net.minecraft.block.state.IBlockState"), int.class);
|
||||
for (int i = 0; i < Character.MAX_VALUE; i++) {
|
||||
try {
|
||||
BlockState state = (BlockState) methodGetByCombinedId.invoke(null, i);
|
||||
if (state.getType() == BlockTypes.AIR) {
|
||||
continue;
|
||||
}
|
||||
PlotBlock plotBlock = new PlotBlock((short) (i & 0xFFF), (byte) (i >> 12 & 0xF));
|
||||
stateArray[i] = state;
|
||||
stateMap.put(state, plotBlock);
|
||||
} catch (Throwable e) {}
|
||||
}
|
||||
PS.debug("Done!");
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static BlockState getBlockState(int id, int data) {
|
||||
if (stateArray == null) {
|
||||
initBlockCache();
|
||||
}
|
||||
return stateArray[id + (data << 12)];
|
||||
}
|
||||
|
||||
public static PlotBlock getPlotBlock(BlockState state) {
|
||||
if (stateMap == null) {
|
||||
initBlockCache();
|
||||
}
|
||||
return stateMap.get(state);
|
||||
}
|
||||
|
||||
public static Location getLocation(final org.spongepowered.api.world.Location<World> block) {
|
||||
return getLocation(block.getExtent().getName(), block);
|
||||
}
|
||||
|
||||
public static Location getLocationFull(final Entity player) {
|
||||
final String world = player.getWorld().getName();
|
||||
final Vector3d rot = player.getRotation();
|
||||
final float[] pitchYaw = MathMan.getPitchAndYaw((float) rot.getX(), (float) rot.getY(), (float) rot.getZ());
|
||||
final org.spongepowered.api.world.Location loc = player.getLocation();
|
||||
final Vector3i pos = loc.getBlockPosition();
|
||||
return new Location(world, pos.getX(), pos.getY(), pos.getZ(), pitchYaw[1], pitchYaw[0]);
|
||||
}
|
||||
|
||||
private static Player lastPlayer = null;
|
||||
private static PlotPlayer lastPlotPlayer = null;
|
||||
|
||||
public static PlotPlayer getPlayer(final Player player) {
|
||||
if (player == lastPlayer) {
|
||||
return lastPlotPlayer;
|
||||
}
|
||||
final String name = player.getName();
|
||||
final PlotPlayer pp = UUIDHandler.getPlayer(name);
|
||||
if (pp != null) {
|
||||
return pp;
|
||||
}
|
||||
lastPlotPlayer = new SpongePlayer(player);
|
||||
UUIDHandler.getPlayers().put(name, lastPlotPlayer);
|
||||
lastPlayer = player;
|
||||
return lastPlotPlayer;
|
||||
}
|
||||
|
||||
public static Player getPlayer(final PlotPlayer player) {
|
||||
if (player instanceof SpongePlayer) {
|
||||
return ((SpongePlayer) player).player;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static World lastWorld;
|
||||
private static String last;
|
||||
|
||||
public static World getWorld(final String world) {
|
||||
if (world == last) {
|
||||
return lastWorld;
|
||||
}
|
||||
final Optional<World> optional = Sponge.getServer().getWorld(world);
|
||||
if (!optional.isPresent()) {
|
||||
return null;
|
||||
}
|
||||
return optional.get();
|
||||
}
|
||||
|
||||
public static void removePlayer(final String player) {
|
||||
lastPlayer = null;
|
||||
lastPlotPlayer = null;
|
||||
}
|
||||
|
||||
public static Location getLocation(final String world, final org.spongepowered.api.world.Location spawn) {
|
||||
return new Location(world, spawn.getBlockX(), spawn.getBlockY(), spawn.getBlockZ());
|
||||
}
|
||||
|
||||
public static String getWorldName(final org.spongepowered.api.world.Location origin) {
|
||||
final Extent extent = origin.getExtent();
|
||||
if (extent == lastWorld) {
|
||||
return last;
|
||||
}
|
||||
if (extent instanceof World) {
|
||||
lastWorld = (World) extent;
|
||||
last = ((World) extent).getName();
|
||||
return last;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static org.spongepowered.api.world.Location getLocation(final Location loc) {
|
||||
final Optional<World> world = SpongeMain.THIS.getServer().getWorld(loc.getWorld());
|
||||
if (!world.isPresent()) {
|
||||
return null;
|
||||
}
|
||||
return new org.spongepowered.api.world.Location(world.get(), loc.getX(), loc.getY(), loc.getZ());
|
||||
}
|
||||
|
||||
public static Location getLocation(String world, Vector3i position) {
|
||||
return new Location(world, position.getX(), position.getY(), position.getZ());
|
||||
}
|
||||
|
||||
public static Location getLocation(String world, Vector3d position) {
|
||||
return new Location(world, MathMan.roundInt(position.getX()), MathMan.roundInt(position.getY()), MathMan.roundInt(position.getZ()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBlockSolid(final PlotBlock block) {
|
||||
final BlockState state = SpongeUtil.getBlockState(block.id, block.data);
|
||||
final BlockType type = state.getType();
|
||||
// TODO check if solid;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StringComparison<PlotBlock>.ComparisonResult getClosestBlock(String name) {
|
||||
try {
|
||||
|
||||
double match;
|
||||
short id;
|
||||
byte data;
|
||||
final String[] split = name.split(":");
|
||||
if (split.length == 2) {
|
||||
data = Byte.parseByte(split[1]);
|
||||
name = split[0];
|
||||
} else {
|
||||
data = 0;
|
||||
}
|
||||
if (MathMan.isInteger(split[0])) {
|
||||
id = Short.parseShort(split[0]);
|
||||
match = 0;
|
||||
} else {
|
||||
List<BlockType> types = ReflectionUtils.<BlockType> getStaticFields(BlockTypes.class);
|
||||
final StringComparison<BlockType>.ComparisonResult comparison = new StringComparison<BlockType>(name, types.toArray(new BlockType[types.size()])) {
|
||||
@Override
|
||||
public String getString(final BlockType type) {
|
||||
return type.getId();
|
||||
};
|
||||
}.getBestMatchAdvanced();
|
||||
match = comparison.match;
|
||||
id = SpongeUtil.getPlotBlock(comparison.best.getDefaultState()).id;
|
||||
}
|
||||
final PlotBlock block = new PlotBlock(id, data);
|
||||
final StringComparison<PlotBlock> outer = new StringComparison<PlotBlock>();
|
||||
return outer.new ComparisonResult(match, block);
|
||||
|
||||
} catch (final Exception e) {}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getClosestMatchingName(final PlotBlock block) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getBiomeList() {
|
||||
if (biomes == null) {
|
||||
initBiomeCache();
|
||||
}
|
||||
return biomeMap.keySet().toArray(new String[biomeMap.size()]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addItems(final String world, final PlotItem items) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBiomeFromString(final String biome) {
|
||||
if (biomes == null) {
|
||||
initBiomeCache();
|
||||
}
|
||||
return biomeMap.get(biome.toUpperCase());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBiome(final String world, final int x, final int z) {
|
||||
return SpongeUtil.getWorld(world).getBiome(x, z).getName().toUpperCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlotBlock getBlock(final Location loc) {
|
||||
final BlockState state = SpongeUtil.getWorld(loc.getWorld()).getBlock(loc.getX(), loc.getY(), loc.getZ());
|
||||
return SpongeUtil.getPlotBlock(state);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getSpawn(final String world) {
|
||||
final World worldObj = SpongeUtil.getWorld(world);
|
||||
worldObj.getSpawnLocation();
|
||||
final Location result = SpongeUtil.getLocation(world, SpongeUtil.getWorld(world).getSpawnLocation());
|
||||
result.setY(getHighestBlock(world, result.getX(), result.getZ()));
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getSign(final Location loc) {
|
||||
final World world = SpongeUtil.getWorld(loc.getWorld());
|
||||
final Optional<TileEntity> block = world.getTileEntity(loc.getX(), loc.getY(), loc.getZ());
|
||||
if (!block.isPresent()) {
|
||||
return null;
|
||||
}
|
||||
final TileEntity tile = block.get();
|
||||
if (!(tile instanceof Sign)) {
|
||||
return null;
|
||||
}
|
||||
final Sign sign = (Sign) tile;
|
||||
final Optional<SignData> optional = sign.getOrCreate(SignData.class);
|
||||
if (!optional.isPresent()) {
|
||||
return null;
|
||||
}
|
||||
final String[] result = new String[4];
|
||||
ListValue<Text> lines = optional.get().lines();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
result[i] = lines.get(i).toString();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWorld(final String world) {
|
||||
return SpongeUtil.getWorld(world) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMainWorld() {
|
||||
return Sponge.getServer().getWorlds().iterator().next().getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHighestBlock(String worldname, int x, int z) {
|
||||
final World world = SpongeUtil.getWorld(worldname);
|
||||
if (world == null) {
|
||||
return 64;
|
||||
}
|
||||
for (int y = 255; y > 0; y--) {
|
||||
final BlockState block = world.getBlock(x, y, z);
|
||||
if ((block != null) && (block.getType() != BlockTypes.AIR)) {
|
||||
return y + 1;
|
||||
}
|
||||
}
|
||||
return 64;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSign(String worldname, int x, int y, int z, String[] lines) {
|
||||
final World world = SpongeUtil.getWorld(worldname);
|
||||
world.setBlock(x, y, z, BlockTypes.WALL_SIGN.getDefaultState());
|
||||
final Optional<TileEntity> block = world.getTileEntity(x, y, z);
|
||||
if (!block.isPresent()) {
|
||||
return;
|
||||
}
|
||||
final TileEntity tile = block.get();
|
||||
if (!(tile instanceof Sign)) {
|
||||
return;
|
||||
}
|
||||
final Sign sign = (Sign) tile;
|
||||
final List<Text> text = new ArrayList<>(4);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
text.add(Text.of(lines[i]));
|
||||
}
|
||||
try {
|
||||
final Optional<SignData> optional = sign.getOrCreate(SignData.class);
|
||||
if (optional.isPresent()) {
|
||||
final SignData offering = optional.get();
|
||||
offering.lines().set(0, Text.of(lines[0]));
|
||||
offering.lines().set(1, Text.of(lines[1]));
|
||||
offering.lines().set(2, Text.of(lines[2]));
|
||||
offering.lines().set(3, Text.of(lines[3]));
|
||||
sign.offer(offering);
|
||||
}
|
||||
} catch (final NullPointerException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBiomes(String worldname, RegionWrapper region, String biomename) {
|
||||
final World world = SpongeUtil.getWorld(worldname);
|
||||
final BiomeType biome = SpongeUtil.getBiome(biomename);
|
||||
for (int x = region.minX; x <= region.maxX; x++) {
|
||||
for (int z = region.minZ; z <= region.maxZ; z++) {
|
||||
world.setBiome(x, z, biome);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.plotsquared.sponge.util.block;
|
||||
|
||||
public class FastChunk {
|
||||
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.plotsquared.sponge.util.block;
|
||||
|
||||
public class FastQueue extends SlowQueue {
|
||||
// TODO FIXME
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package com.plotsquared.sponge.util.block;
|
||||
|
||||
import org.spongepowered.api.world.Chunk;
|
||||
import org.spongepowered.api.world.extent.MutableBiomeArea;
|
||||
import org.spongepowered.api.world.extent.MutableBlockVolume;
|
||||
|
||||
import com.intellectualcrafters.plot.util.PlotChunk;
|
||||
import com.intellectualcrafters.plot.util.SetQueue.ChunkWrapper;
|
||||
import com.plotsquared.sponge.util.SpongeUtil;
|
||||
|
||||
public class GenChunk extends PlotChunk<Chunk> {
|
||||
|
||||
public boolean modified = false;
|
||||
private final MutableBlockVolume terain;
|
||||
private final MutableBiomeArea biome;
|
||||
private final int bz;
|
||||
private final int bx;
|
||||
|
||||
public GenChunk(MutableBlockVolume terain, MutableBiomeArea biome, ChunkWrapper wrap) {
|
||||
super(wrap);
|
||||
this.bx = wrap.x << 4;
|
||||
this.bz = wrap.z << 4;
|
||||
this.terain = terain;
|
||||
this.biome = biome;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Chunk getChunkAbs() {
|
||||
ChunkWrapper wrap = getChunkWrapper();
|
||||
return SpongeUtil.getWorld(wrap.world).getChunk(wrap.x << 4, 0, wrap.z << 4).orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBiome(int x, int z, int biome) {
|
||||
if (this.biome != null) {
|
||||
this.biome.setBiome(bx + x, bz + z, SpongeUtil.getBiome(biome));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlock(int x, int y, int z, int id, byte data) {
|
||||
terain.setBlock(bx + x, y, bz + z, SpongeUtil.getBlockState(id, data));
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlotChunk clone() {
|
||||
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlotChunk shallowClone() {
|
||||
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package com.plotsquared.sponge.util.block;
|
||||
|
||||
import org.spongepowered.api.world.Chunk;
|
||||
|
||||
import com.intellectualcrafters.plot.object.PlotBlock;
|
||||
import com.intellectualcrafters.plot.util.MainUtil;
|
||||
import com.intellectualcrafters.plot.util.PlotChunk;
|
||||
import com.intellectualcrafters.plot.util.SetQueue.ChunkWrapper;
|
||||
import com.plotsquared.sponge.util.SpongeUtil;
|
||||
|
||||
public class SlowChunk extends PlotChunk<Chunk> {
|
||||
|
||||
public PlotBlock[][] result = new PlotBlock[16][];
|
||||
public int[][] biomes;
|
||||
private PlotBlock lastBlock;
|
||||
|
||||
public SlowChunk(ChunkWrapper chunk) {
|
||||
super(chunk);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Chunk getChunkAbs() {
|
||||
ChunkWrapper loc = getChunkWrapper();
|
||||
return SpongeUtil.getWorld(loc.world).getChunk(loc.x << 4, 0, loc.z << 4).get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBiome(int x, int z, int biome) {
|
||||
if (biomes == null) {
|
||||
biomes = new int[16][16];
|
||||
}
|
||||
biomes[x][z] = biome;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlock(int x, int y, int z, int id, byte data) {
|
||||
if (result[y >> 4] == null) {
|
||||
result[y >> 4] = new PlotBlock[4096];
|
||||
}
|
||||
if (id == lastBlock.id && data == lastBlock.data) {
|
||||
result[MainUtil.CACHE_I[x][y][z]][MainUtil.CACHE_J[x][y][z]] = lastBlock;
|
||||
} else {
|
||||
result[MainUtil.CACHE_I[x][y][z]][MainUtil.CACHE_J[x][y][z]] = new PlotBlock((short) id, data);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlotChunk clone() {
|
||||
SlowChunk toReturn = new SlowChunk(getChunkWrapper());
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
PlotBlock[] matrix = result[i];
|
||||
if (matrix != null) {
|
||||
toReturn.result[i] = new PlotBlock[matrix.length];
|
||||
System.arraycopy(matrix, 0, toReturn.result[i], 0, matrix.length);
|
||||
}
|
||||
}
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlotChunk shallowClone() {
|
||||
SlowChunk toReturn = new SlowChunk(getChunkWrapper());
|
||||
toReturn.result = result;
|
||||
return toReturn;
|
||||
}
|
||||
}
|
@ -0,0 +1,175 @@
|
||||
package com.plotsquared.sponge.util.block;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.spongepowered.api.block.BlockState;
|
||||
import org.spongepowered.api.world.Chunk;
|
||||
|
||||
import com.flowpowered.math.vector.Vector3i;
|
||||
import com.intellectualcrafters.plot.PS;
|
||||
import com.intellectualcrafters.plot.object.ChunkLoc;
|
||||
import com.intellectualcrafters.plot.object.PlotBlock;
|
||||
import com.intellectualcrafters.plot.util.MainUtil;
|
||||
import com.intellectualcrafters.plot.util.PlotChunk;
|
||||
import com.intellectualcrafters.plot.util.PlotQueue;
|
||||
import com.intellectualcrafters.plot.util.SetQueue;
|
||||
import com.intellectualcrafters.plot.util.SetQueue.ChunkWrapper;
|
||||
import com.plotsquared.sponge.util.SpongeUtil;
|
||||
|
||||
public class SlowQueue implements PlotQueue<Chunk> {
|
||||
|
||||
private final ConcurrentHashMap<ChunkWrapper, PlotChunk<Chunk>> blocks = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public boolean setBlock(String world, int x, int y, int z, short id, byte data) {
|
||||
if (y > 255 || y < 0) {
|
||||
return false;
|
||||
}
|
||||
final ChunkWrapper wrap = SetQueue.IMP.new ChunkWrapper(world, x >> 4, z >> 4);
|
||||
x = x & 15;
|
||||
z = z & 15;
|
||||
PlotChunk<Chunk> result = blocks.get(wrap);
|
||||
if (result == null) {
|
||||
result = getChunk(wrap);
|
||||
result.setBlock(x, y, z, id, data);
|
||||
final PlotChunk<Chunk> previous = blocks.put(wrap, result);
|
||||
if (previous == null) {
|
||||
return true;
|
||||
}
|
||||
blocks.put(wrap, previous);
|
||||
result = previous;
|
||||
}
|
||||
result.setBlock(x, y, z, id, data);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setChunk(PlotChunk<Chunk> chunk) {
|
||||
blocks.put(chunk.getChunkWrapper(), chunk);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlotChunk<Chunk> next() {
|
||||
if (!PS.get().isMainThread(Thread.currentThread())) {
|
||||
throw new IllegalStateException("Must be called from main thread!");
|
||||
}
|
||||
try {
|
||||
if (blocks.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
final Iterator<Entry<ChunkWrapper, PlotChunk<Chunk>>> iter = blocks.entrySet().iterator();
|
||||
final PlotChunk<Chunk> toReturn = iter.next().getValue();
|
||||
if (SetQueue.IMP.isWaiting()) {
|
||||
return null;
|
||||
}
|
||||
iter.remove();
|
||||
execute(toReturn);
|
||||
fixLighting(toReturn, true);
|
||||
return toReturn;
|
||||
} catch (final Throwable e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlotChunk<Chunk> next(ChunkWrapper wrap, boolean fixLighting) {
|
||||
if (!PS.get().isMainThread(Thread.currentThread())) {
|
||||
throw new IllegalStateException("Must be called from main thread!");
|
||||
}
|
||||
try {
|
||||
if (blocks.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
final PlotChunk<Chunk> toReturn = blocks.remove(wrap);
|
||||
if (toReturn == null) {
|
||||
return null;
|
||||
}
|
||||
execute(toReturn);
|
||||
fixLighting(toReturn, fixLighting);
|
||||
return toReturn;
|
||||
} catch (final Throwable e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
blocks.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* This should be overriden by any specialized queues
|
||||
* @param pc
|
||||
*/
|
||||
public void execute(PlotChunk<Chunk> pc) {
|
||||
SlowChunk sc = (SlowChunk) pc;
|
||||
Chunk chunk = pc.getChunk();
|
||||
chunk.loadChunk(true);
|
||||
Vector3i min = chunk.getBlockMin();
|
||||
int bx = min.getX();
|
||||
int bz = min.getZ();
|
||||
for (int i = 0; i < sc.result.length; i++) {
|
||||
PlotBlock[] result2 = sc.result[i];
|
||||
if (result2 == null) {
|
||||
continue;
|
||||
}
|
||||
for (int j = 0; j < 4096; j++) {
|
||||
final int x = MainUtil.x_loc[i][j];
|
||||
final int y = MainUtil.y_loc[i][j];
|
||||
final int z = MainUtil.z_loc[i][j];
|
||||
PlotBlock newBlock = result2[j];
|
||||
BlockState state = SpongeUtil.getBlockState(newBlock.id, newBlock.data);
|
||||
chunk.setBlock(bx + x, y, bz + z, state, false);
|
||||
}
|
||||
}
|
||||
int[][] biomes = sc.biomes;
|
||||
if (biomes != null) {
|
||||
for (int x = 0; x < 16; x++) {
|
||||
int[] array = biomes[x];
|
||||
if (array == null) {
|
||||
continue;
|
||||
}
|
||||
for (int z = 0; z < 16; z++) {
|
||||
int biome = array[z];
|
||||
if (biome == 0) {
|
||||
continue;
|
||||
}
|
||||
chunk.setBiome(bx + x, bz + z, SpongeUtil.getBiome(biome));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This should be overriden by any specialized queues
|
||||
* @param wrap
|
||||
*/
|
||||
@Override
|
||||
public PlotChunk<Chunk> getChunk(ChunkWrapper wrap) {
|
||||
return new SlowChunk(wrap);
|
||||
}
|
||||
|
||||
/**
|
||||
* This should be overriden by any specialized queues
|
||||
* @param fixAll
|
||||
*/
|
||||
@Override
|
||||
public boolean fixLighting(PlotChunk<Chunk> chunk, boolean fixAll) {
|
||||
// Do nothing
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* This should be overriden by any specialized queues
|
||||
* @param locs
|
||||
*/
|
||||
@Override
|
||||
public void sendChunk(String world, Collection<ChunkLoc> locs) {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user