PlotSquared/Core/src/main/java/com/plotsquared/core/util/WorldUtil.java

220 lines
9.3 KiB
Java
Raw Normal View History

/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* PlotSquared plot management system for Minecraft
* Copyright (C) 2020 IntellectualSites
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2020-04-15 21:26:54 +02:00
package com.plotsquared.core.util;
2016-02-10 19:59:51 +01:00
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.location.Location;
import com.plotsquared.core.player.PlotPlayer;
2020-04-30 12:01:52 +02:00
import com.plotsquared.core.plot.Plot;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.util.task.RunnableVal;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.IntTag;
import com.sk89q.jnbt.NBTInputStream;
import com.sk89q.jnbt.NBTOutputStream;
import com.sk89q.jnbt.Tag;
2019-11-04 18:44:23 +01:00
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.world.biome.BiomeType;
2019-11-04 20:58:24 +01:00
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.entity.EntityType;
import org.jetbrains.annotations.NotNull;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.Map;
2017-03-23 01:10:29 +01:00
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.IntConsumer;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
2016-02-10 19:59:51 +01:00
public abstract class WorldUtil {
public static WorldUtil IMP;
2018-08-10 17:01:10 +02:00
2016-02-10 19:59:51 +01:00
public abstract String getMainWorld();
2016-03-29 21:47:59 +02:00
public abstract boolean isWorld(String worldName);
2016-03-23 02:41:37 +01:00
public abstract void getSign(Location location, Consumer<String[]> result);
/**
* @deprecated May result in synchronous chunk loading
*/
2020-04-30 12:01:52 +02:00
@Deprecated public abstract String[] getSignSynchronous(Location location);
2016-02-10 19:59:51 +01:00
public abstract Location getSpawn(String world);
2016-03-23 02:41:37 +01:00
public abstract void setSpawn(Location location);
public abstract void saveWorld(String world);
2018-08-10 17:01:10 +02:00
2019-11-04 20:55:55 +01:00
public abstract String getClosestMatchingName(BlockState plotBlock);
2018-08-10 17:01:10 +02:00
2019-11-04 20:55:55 +01:00
public abstract boolean isBlockSolid(BlockState block);
2018-08-10 17:01:10 +02:00
2019-11-04 20:55:55 +01:00
public abstract StringComparison<BlockState>.ComparisonResult getClosestBlock(String name);
2018-08-10 17:01:10 +02:00
public abstract void getBiome(String world, int x, int z, Consumer<BiomeType> result);
/**
* @deprecated May result in synchronous chunk loading
*/
2020-04-30 12:01:52 +02:00
@Deprecated public abstract BiomeType getBiomeSynchronous(String world, int x, int z);
public abstract void getBlock(Location location, Consumer<BlockState> result);
2018-08-10 17:01:10 +02:00
/**
* @deprecated May result in synchronous chunk loading
*/
2020-04-30 12:01:52 +02:00
@Deprecated public abstract BlockState getBlockSynchronous(Location location);
2018-08-10 17:01:10 +02:00
public abstract void getHighestBlock(String world, int x, int z, final IntConsumer result);
2018-08-10 17:01:10 +02:00
/**
* @deprecated May result in synchronous chunk loading
*/
2020-04-30 12:01:52 +02:00
@Deprecated public abstract int getHighestBlockSynchronous(String world, int x, int z);
2018-08-10 17:01:10 +02:00
2016-02-10 19:59:51 +01:00
public abstract void setSign(String world, int x, int y, int z, String[] lines);
2018-08-10 17:01:10 +02:00
public abstract void setBiomes(String world, CuboidRegion region, BiomeType biome);
public abstract com.sk89q.worldedit.world.World getWeWorld(String world);
2020-04-30 12:01:52 +02:00
public void upload(@NotNull final Plot plot, UUID uuid, String file,
RunnableVal<URL> whenDone) {
plot.getHome(home -> MainUtil.upload(uuid, file, "zip", new RunnableVal<OutputStream>() {
2018-08-10 17:01:10 +02:00
@Override public void run(OutputStream output) {
try (final ZipOutputStream zos = new ZipOutputStream(output)) {
2017-03-23 01:10:29 +01:00
File dat = getDat(plot.getWorldName());
Location spawn = getSpawn(plot.getWorldName());
if (dat != null) {
2016-03-23 02:41:37 +01:00
ZipEntry ze = new ZipEntry("world" + File.separator + dat.getName());
zos.putNextEntry(ze);
2018-08-10 17:01:10 +02:00
try (NBTInputStream nis = new NBTInputStream(
new GZIPInputStream(new FileInputStream(dat)))) {
CompoundTag tag = (CompoundTag) nis.readNamedTag().getTag();
CompoundTag data = (CompoundTag) tag.getValue().get("Data");
Map<String, Tag> map = ReflectionUtils.getMap(data.getValue());
map.put("SpawnX", new IntTag(home.getX()));
map.put("SpawnY", new IntTag(home.getY()));
map.put("SpawnZ", new IntTag(home.getZ()));
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
try (NBTOutputStream out = new NBTOutputStream(
new GZIPOutputStream(baos, true))) {
//TODO Find what this should be called
out.writeNamedTag("Schematic????", tag);
}
zos.write(baos.toByteArray());
}
}
}
setSpawn(spawn);
byte[] buffer = new byte[1024];
for (Plot current : plot.getConnectedPlots()) {
2016-03-23 02:41:37 +01:00
Location bot = current.getBottomAbs();
Location top = current.getTopAbs();
int brx = bot.getX() >> 9;
int brz = bot.getZ() >> 9;
int trx = top.getX() >> 9;
int trz = top.getZ() >> 9;
Pull/2693 (#2694) * Commit WIP flag work. * More ported flag types, and additions to the flag API. * Make PlotFlag more generic to allow generic flag creation * Pull Captions methods into a Caption interface. * Port MusicFlag * Port flight flag * Port UntrustedVisitFlag * Port DenyExitFlag * Remove paper suggestion * Make ListFlag lists immutable * Update Flag containers. Add javadocs. Add missing methods. * Port description flag * Port greeting and farewell flags * Port weather flag * Move getExample implementation to BooleanFlag * Port reserved flags * Port all boolean flags. * Remove unused flag types * Invert liquid-flow flag * Find the real (legacy) flag name * Change NOITFY -> NOTIFY in Captions * Make IntegerFlag extendable * Port integer flags * Update Flag command to current API state * Begin remaking flag command * Create DoubleFlag + extract common parsing stuff * Supply arguments in flag parse exceptions * Implement missing flag subcommands * Update Flag command to current API state * Implement PriceFlag * Port deny-teleport * Port gamemode flags * Port BreakFlag * Port PlaceFlag * Port UseFlag * Remove old unused flag constants * Port blocked-cmds flag * Fix entity util * Port TimeFlag * Use CaptionUtility for formatting * Port keep flag * Fix imports * Reformat code * Remove unused classes * Fix MainUtil.java * Remove FlagCmd * Add flag info header and footer * Comment out flag related stuff in SchematicHandler * Remove FlagManager * Finalize Plot.java * Finalize PlotArea.java * Finalize PlotListener * Fix API issues * Fix a bunch of compile errors * Fix `/plot flag remove` * Fix initialization of GlobalFlagContainer * Apply API changes to events * Update SQLManager to new API * Invert default value for DenyExitFlag * Replace flag.getValue().toString() with flag.toString() * Make FlagContainer instance in Plot final * Fix various command issues * Clean up PlotSettings * Don't show internal flags in flag list * Fix `/plot flag add` * Remove the info inventory as it's 100% broken * Add plot info entries and fix up the default format * Fix default flag state in Captions * 781c200 part 2 * Fix odd grammar in captions * Fix odd grammar in captions v2 * Add create table statements for plot_flags * Remove old flag references in SQLManager * Use the new plot_flags table * Add tab completion to `/plot flag` * Improve parse error handling * Make flag permission check recognize parse exceptions * Initial progress towards flag conversion * Fix minor issues * Don't validate flags during flag conversion * Allow unrecognized flags to be parsed * Filter out internal flags from command sugguestions * Use the wrong caption when there's no plot description set * Limit command suggestions for boolean flags * Make blocktypelistflags accept blockcategories * Require categories to be prefixed with '#' and fix some minor display issues * Fix plot database conversion * Update PlotFlagEvent.java Updated return description * Fix command annotation wrapping * Add FLAG_UPDATE event for FlagContainer listeners * Make getParentContainer nullable * Document castUnsafe in FlagContainer * Document FlagContainer constructors * Add missing documentation to FlagContainer * Document FlagParseException * Fix wording in FlagContainer javadoc * Document InternalFlag * Document PlotFlag * Minor changes * Remove revisit comments Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com> Co-authored-by: NotMyFault <mc.cache@web.de> Co-authored-by: Matt <4009945+MattBDev@users.noreply.github.com>
2020-02-24 18:42:02 +01:00
Set<BlockVector2> files =
ChunkManager.manager.getChunkChunks(bot.getWorld());
2019-11-04 18:44:23 +01:00
for (BlockVector2 mca : files) {
Pull/2693 (#2694) * Commit WIP flag work. * More ported flag types, and additions to the flag API. * Make PlotFlag more generic to allow generic flag creation * Pull Captions methods into a Caption interface. * Port MusicFlag * Port flight flag * Port UntrustedVisitFlag * Port DenyExitFlag * Remove paper suggestion * Make ListFlag lists immutable * Update Flag containers. Add javadocs. Add missing methods. * Port description flag * Port greeting and farewell flags * Port weather flag * Move getExample implementation to BooleanFlag * Port reserved flags * Port all boolean flags. * Remove unused flag types * Invert liquid-flow flag * Find the real (legacy) flag name * Change NOITFY -> NOTIFY in Captions * Make IntegerFlag extendable * Port integer flags * Update Flag command to current API state * Begin remaking flag command * Create DoubleFlag + extract common parsing stuff * Supply arguments in flag parse exceptions * Implement missing flag subcommands * Update Flag command to current API state * Implement PriceFlag * Port deny-teleport * Port gamemode flags * Port BreakFlag * Port PlaceFlag * Port UseFlag * Remove old unused flag constants * Port blocked-cmds flag * Fix entity util * Port TimeFlag * Use CaptionUtility for formatting * Port keep flag * Fix imports * Reformat code * Remove unused classes * Fix MainUtil.java * Remove FlagCmd * Add flag info header and footer * Comment out flag related stuff in SchematicHandler * Remove FlagManager * Finalize Plot.java * Finalize PlotArea.java * Finalize PlotListener * Fix API issues * Fix a bunch of compile errors * Fix `/plot flag remove` * Fix initialization of GlobalFlagContainer * Apply API changes to events * Update SQLManager to new API * Invert default value for DenyExitFlag * Replace flag.getValue().toString() with flag.toString() * Make FlagContainer instance in Plot final * Fix various command issues * Clean up PlotSettings * Don't show internal flags in flag list * Fix `/plot flag add` * Remove the info inventory as it's 100% broken * Add plot info entries and fix up the default format * Fix default flag state in Captions * 781c200 part 2 * Fix odd grammar in captions * Fix odd grammar in captions v2 * Add create table statements for plot_flags * Remove old flag references in SQLManager * Use the new plot_flags table * Add tab completion to `/plot flag` * Improve parse error handling * Make flag permission check recognize parse exceptions * Initial progress towards flag conversion * Fix minor issues * Don't validate flags during flag conversion * Allow unrecognized flags to be parsed * Filter out internal flags from command sugguestions * Use the wrong caption when there's no plot description set * Limit command suggestions for boolean flags * Make blocktypelistflags accept blockcategories * Require categories to be prefixed with '#' and fix some minor display issues * Fix plot database conversion * Update PlotFlagEvent.java Updated return description * Fix command annotation wrapping * Add FLAG_UPDATE event for FlagContainer listeners * Make getParentContainer nullable * Document castUnsafe in FlagContainer * Document FlagContainer constructors * Add missing documentation to FlagContainer * Document FlagParseException * Fix wording in FlagContainer javadoc * Document InternalFlag * Document PlotFlag * Minor changes * Remove revisit comments Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com> Co-authored-by: NotMyFault <mc.cache@web.de> Co-authored-by: Matt <4009945+MattBDev@users.noreply.github.com>
2020-02-24 18:42:02 +01:00
if (mca.getX() >= brx && mca.getX() <= trx && mca.getZ() >= brz
&& mca.getZ() <= trz) {
final File file =
getMcr(plot.getWorldName(), mca.getX(), mca.getZ());
if (file != null) {
//final String name = "r." + (x - cx) + "." + (z - cz) + ".mca";
String name = file.getName();
2018-08-10 17:01:10 +02:00
final ZipEntry ze = new ZipEntry(
"world" + File.separator + "region" + File.separator
+ name);
zos.putNextEntry(ze);
try (FileInputStream in = new FileInputStream(file)) {
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
}
2017-03-23 01:10:29 +01:00
zos.closeEntry();
}
}
}
}
zos.closeEntry();
zos.flush();
zos.finish();
} catch (IOException e) {
e.printStackTrace();
}
}
}, whenDone));
}
2016-03-23 02:41:37 +01:00
public File getDat(String world) {
2018-08-10 17:01:10 +02:00
File file = new File(
PlotSquared.get().IMP.getWorldContainer() + File.separator + world + File.separator
2018-08-10 17:01:10 +02:00
+ "level.dat");
if (file.exists()) {
return file;
}
return null;
}
2016-03-23 02:41:37 +01:00
public File getMcr(String world, int x, int z) {
File file = new File(PlotSquared.get().IMP.getWorldContainer(),
2018-08-10 17:01:10 +02:00
world + File.separator + "region" + File.separator + "r." + x + '.' + z + ".mca");
if (file.exists()) {
return file;
}
return null;
}
2018-12-17 20:57:21 +01:00
2019-11-04 20:55:55 +01:00
public abstract boolean isBlockSame(BlockState block1, BlockState block2);
public abstract PlotPlayer wrapPlayer(UUID uuid);
public abstract double getHealth(PlotPlayer player);
public abstract void setHealth(PlotPlayer player, double health);
public abstract int getFoodLevel(PlotPlayer player);
public abstract void setFoodLevel(PlotPlayer player, int foodLevel);
public abstract Set<EntityType> getTypesInCategory(final String category);
2016-02-10 19:59:51 +01:00
}