Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PlotHider to PlotSquared v6 #29

Merged
merged 4 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@ on: ["pull_request", "push"]

jobs:
build:
strategy:
matrix:
java: ["8", "11"]
os: ["ubuntu-latest"]
runs-on: "${{ matrix.os }}"
runs-on: "ubuntu-latest"
steps:
- name: "Checkout Repository"
uses: "actions/[email protected]"
- name: "Setup JDK ${{ matrix.java }}"
uses: "actions/setup-java@v2"
- name: "Setup JDK 16"
uses: "actions/setup-java@v2.1.0"
with:
distribution: "adopt"
java-version: "${{ matrix.java }}"
java-version: "16"
- name: "Clean Build"
run: "./gradlew clean build"
run: "./gradlew clean build"
19 changes: 11 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,34 @@ import net.minecrell.pluginyml.bukkit.BukkitPluginDescription
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

plugins {
id("java")
id("java-library")
java
`java-library`

id("net.minecrell.plugin-yml.bukkit") version "0.4.0"
id("com.github.johnrengelman.shadow") version "7.0.0"
}

configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = sourceCompatibility
the<JavaPluginExtension>().toolchain {
languageVersion.set(JavaLanguageVersion.of(16))
}

version = "4.0.1"
version = "5.0.0"

repositories {
mavenCentral()
maven { url = uri("https://mvn.intellectualsites.com/content/groups/public/") }
maven { url = uri("https://hub.spigotmc.org/nexus/content/repositories/snapshots/") }
maven { url = uri("https://repo.dmulloy2.net/nexus/repository/public/") }
maven { url = uri("https://maven.enginehub.org/repo/") }
maven { url = uri("https://oss.sonatype.org/content/repositories/snapshots/") }
}

dependencies {
compileOnlyApi("org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT")
compileOnly("com.plotsquared:PlotSquared-Core:5.13.11")
compileOnly("com.plotsquared:PlotSquared-Bukkit:5.13.11") { isTransitive = false }
compileOnly("com.plotsquared:PlotSquared-Core:6.0.0-SNAPSHOT")
compileOnly("com.plotsquared:PlotSquared-Bukkit:6.0.0-SNAPSHOT") { isTransitive = false }
compileOnly("com.comphenix.protocol:ProtocolLib:4.6.0")
compileOnly("com.sk89q.worldedit:worldedit-core:7.2.5")
implementation("org.bstats:bstats-bukkit:2.2.1")
implementation("org.bstats:bstats-base:2.2.1")
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/plotsquared/plothider/HideFlag.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.plotsquared.plothider;

import com.plotsquared.core.configuration.StaticCaption;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.plot.flag.types.BooleanFlag;
import org.jetbrains.annotations.NotNull;

public class HideFlag extends BooleanFlag<HideFlag> {

public static final HideFlag HIDE_FLAG_TRUE = new HideFlag(true);
public static final HideFlag HIDE_FLAG_FALSE = new HideFlag(false);

protected HideFlag(boolean value) {
super(value, new StaticCaption("hide", false));
private HideFlag(boolean value) {
super(value, TranslatableCaption.of(PlotHiderPlugin.PLOT_HIDER_NAMESPACE, "flags.flag_description_hide"));
}

@Override
protected HideFlag flagOf(Boolean value) {
protected HideFlag flagOf(@NotNull Boolean value) {
return value ? HIDE_FLAG_TRUE : HIDE_FLAG_FALSE;
}

Expand Down
45 changes: 25 additions & 20 deletions src/main/java/com/plotsquared/plothider/PacketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.plotsquared.bukkit.util.BukkitUtil;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.location.Location;
import com.plotsquared.core.location.World;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotArea;
Expand All @@ -38,18 +39,18 @@ public class PacketHandler {
new PacketAdapter(main, ListenerPriority.NORMAL, PacketType.Play.Server.BLOCK_CHANGE) {
public void onPacketSending(PacketEvent event) {
Player player = event.getPlayer();
PlotPlayer<?> pp = BukkitUtil.getPlayer(player);
PlotPlayer<?> pp = BukkitUtil.adapt(player);
if (Permissions.hasPermission(pp, "plots.plothider.bypass")) { // Admin bypass
return;
}
String world = pp.getLocation().getWorld();
if (!PlotSquared.get().hasPlotArea(world)) { // Not a plot area
World<?> world = pp.getLocation().getWorld();
if (!hasPlotArea(world)) { // Not a plot area
return;
}
PacketContainer packet = event.getPacket();
StructureModifier<BlockPosition> positions = packet.getBlockPositionModifier();
BlockPosition position = positions.read(0);
Location loc = new Location(world, position.getX(), 0, position.getZ());
Location loc = Location.at(world, position.getX(), 0, position.getZ());
Plot plot = loc.getOwnedPlot();
if (plot != null && (plot.isDenied(pp.getUUID()) || (!plot.isAdded(pp.getUUID())
&& plot.getFlag(HideFlag.class)))) {
Expand All @@ -63,12 +64,12 @@ public void onPacketSending(PacketEvent event) {
@Override
public void onPacketSending(PacketEvent event) {
Player player = event.getPlayer();
PlotPlayer<?> pp = BukkitUtil.getPlayer(player);
PlotPlayer<?> pp = BukkitUtil.adapt(player);
if (Permissions.hasPermission(pp, "plots.plothider.bypass")) { // Admin bypass
return;
}
String world = pp.getLocation().getWorld();
if (!PlotSquared.get().hasPlotArea(world)) { // Not a plot area
World<?> world = pp.getLocation().getWorld();
if (!hasPlotArea(world)) { // Not a plot area
return;
}
PacketContainer packet = event.getPacket();
Expand All @@ -79,10 +80,10 @@ public void onPacketSending(PacketEvent event) {
int cz = chunk.getChunkZ();
int bx = cx << 4;
int bz = cz << 4;
Location corner1 = new Location(world, bx, 0, bz);
Location corner2 = new Location(world, bx + 15, 0, bz);
Location corner3 = new Location(world, bx, 0, bz + 15);
Location corner4 = new Location(world, bx + 15, 0, bz + 15);
Location corner1 = Location.at(world, bx, 0, bz);
Location corner2 = Location.at(world, bx + 15, 0, bz);
Location corner3 = Location.at(world, bx, 0, bz + 15);
Location corner4 = Location.at(world, bx + 15, 0, bz + 15);
Plot plot1 = corner1.getOwnedPlot();
Plot plot2 = corner2.getOwnedPlot();
Plot plot3 = corner3.getOwnedPlot();
Expand Down Expand Up @@ -128,7 +129,7 @@ public void onPacketSending(PacketEvent event) {
// Binary operators give section-relative coordinates.
int x = bx + (change >>> 8 & 15);
int z = bz + (change >>> 4 & 15);
Plot current = area.getOwnedPlot(new Location(world, x, 0, z));
Plot current = area.getOwnedPlot(Location.at(world, x, 0, z));
if (current == null) {
continue;
}
Expand All @@ -152,13 +153,13 @@ public void onPacketSending(PacketEvent event) {
new PacketAdapter(main, ListenerPriority.NORMAL, PacketType.Play.Server.MAP_CHUNK) {
public void onPacketSending(PacketEvent event) {
Player player = event.getPlayer();
PlotPlayer<?> pp = BukkitUtil.getPlayer(player);
PlotPlayer<?> pp = BukkitUtil.adapt(player);
if (Permissions.hasPermission(pp, "plots.plothider.bypass")) { // Admin bypass
return;
}

String world = pp.getLocation().getWorld();
if (!PlotSquared.get().hasPlotArea(world)) { // Not a plot area
World<?> world = pp.getLocation().getWorld();
if (!hasPlotArea(world)) { // Not a plot area
return;
}

Expand All @@ -173,10 +174,10 @@ public void onPacketSending(PacketEvent event) {
int bx = cx << 4;
int bz = cz << 4;

Location corner1 = new Location(world, bx, 0, bz);
Location corner2 = new Location(world, bx + 15, 0, bz);
Location corner3 = new Location(world, bx, 0, bz + 15);
Location corner4 = new Location(world, bx + 15, 0, bz + 15);
Location corner1 = Location.at(world, bx, 0, bz);
Location corner2 = Location.at(world, bx + 15, 0, bz);
Location corner3 = Location.at(world, bx, 0, bz + 15);
Location corner4 = Location.at(world, bx + 15, 0, bz + 15);
Plot plot1 = corner1.getOwnedPlot();
Plot plot2 = corner2.getOwnedPlot();
Plot plot3 = corner3.getOwnedPlot();
Expand Down Expand Up @@ -264,7 +265,7 @@ public void onPacketSending(PacketEvent event) {
int z;
for (int x = 0; x < 16; x++) {
for (z = 0; z < 16; z++) {
Location loc = new Location(world, bx + x, 0, bz + z);
Location loc = Location.at(world, bx + x, 0, bz + z);
Plot current = area.getOwnedPlot(loc);
if (current != null) {
if ((current == plot1) || (current == plot2) || (current
Expand Down Expand Up @@ -296,6 +297,10 @@ public void onPacketSending(PacketEvent event) {
});
}

private boolean hasPlotArea(World<?> world) {
return PlotSquared.get().getPlotAreaManager().hasPlotArea(world.getName());
}

private int readVarInt(InputStream stream) throws IOException {
int i = 0;
int j = 0;
Expand Down
49 changes: 47 additions & 2 deletions src/main/java/com/plotsquared/plothider/PlotHiderPlugin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.plotsquared.plothider;

import com.plotsquared.bukkit.util.BukkitUtil;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.caption.CaptionMap;
import com.plotsquared.core.configuration.caption.load.CaptionLoader;
import com.plotsquared.core.configuration.caption.load.DefaultCaptionProvider;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.flag.GlobalFlagContainer;
Expand All @@ -14,21 +19,61 @@
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;
import java.util.logging.Level;
import java.util.regex.Pattern;

public class PlotHiderPlugin extends JavaPlugin implements Listener {

public static final String PLOT_HIDER_NAMESPACE = "plothider";
private static final int BSTATS_ID = 6412;

@Override
public void onEnable() {
new PacketHandler(this);
Bukkit.getPluginManager().registerEvents(this, this);
GlobalFlagContainer.getInstance().addFlag(new HideFlag(false));
GlobalFlagContainer.getInstance().addFlag(HideFlag.HIDE_FLAG_FALSE);
try {
loadCaptions();
} catch (IOException e) {
getLogger().log(Level.SEVERE, "Failed to load captions", e);
}
new Metrics(this, BSTATS_ID);
}

private void loadCaptions() throws IOException {
Path msgFilePath = getDataFolder().toPath().resolve("lang").resolve("messages_en.json");
if (!Files.exists(msgFilePath)) {
this.saveResource("lang/messages_en.json", false);
}
CaptionLoader captionLoader = CaptionLoader.of(
Locale.ENGLISH,
CaptionLoader.patternExtractor(Pattern.compile("messages_(.*)\\.json")),
DefaultCaptionProvider.forClassLoaderFormatString(
this.getClass().getClassLoader(),
"lang/messages_%s.json"
),
PLOT_HIDER_NAMESPACE
);
CaptionMap captionMap;
if (Settings.Enabled_Components.PER_USER_LOCALE) {
captionMap = captionLoader.loadAll(getDataFolder().toPath().resolve("lang"));
} else {
String fileName = "messages_" + Settings.Enabled_Components.DEFAULT_LOCALE + ".json";
captionMap = captionLoader.loadSingle(getDataFolder().toPath().resolve("lang").resolve(fileName));
}
PlotSquared.get().registerCaptionMap(PLOT_HIDER_NAMESPACE, captionMap);
getLogger().info("Loaded caption map for namespace '" + PLOT_HIDER_NAMESPACE + "': "
+ captionMap.getClass().getCanonicalName());
}

@EventHandler
public void onTeleport(PlayerTeleportEvent event) {
Player player = event.getPlayer();
PlotPlayer<?> pp = BukkitUtil.getPlayer(player);
PlotPlayer<?> pp = BukkitUtil.adapt(player);
if (Permissions.hasPermission(pp, "plots.plothider.bypass")) {
return;
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/lang/messages_en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"flags.flag_description_hide": "<gray>Set to `true` to hide the plot from other players.</gray>"
}