Skip to content

Commit

Permalink
refactor and add couch support
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianvld committed Dec 3, 2020
1 parent aa6779f commit 50ab18c
Show file tree
Hide file tree
Showing 13 changed files with 466 additions and 142 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.kristianvld.angeltrophies;

import com.github.kristianvld.angeltrophies.util.C;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/com/github/kristianvld/angeltrophies/External.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,42 @@

public class External {

private boolean worldGuard;
private boolean angelProtect;
private final boolean worldGuard;
private final boolean angelProtect;

public External() {
worldGuard = Bukkit.getPluginManager().isPluginEnabled("WorldGuard");
if (worldGuard) {
Main.getInstance().getLogger().info("Found WorldGuard, will be using hook to check for external permissions.");
}
angelProtect = Bukkit.getPluginManager().isPluginEnabled("AngelProtect");
if (angelProtect) {
Main.getInstance().getLogger().info("Found AngelProtect, will be using hook to check for external permissions.");
}
}

public boolean canBuild(Player player, Block block) {
if (!canBuildAngelProtect(player, block)) {
return false;
}
if (!canBuildWorldGuard(player, block)) {
return false;
}
return true;
return canBuildWorldGuard(player, block);
}

public boolean canBuildWorldGuard(Player player, Block block) {
if (worldGuard) {

LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
LocalPlayer wgPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
WorldGuardPlatform platform = WorldGuard.getInstance().getPlatform();

if (platform.getSessionManager().hasBypass(localPlayer, localPlayer.getWorld())) {
if (platform.getSessionManager().hasBypass(wgPlayer, wgPlayer.getWorld())) {
return true; // has bypass permission
}

com.sk89q.worldedit.util.Location loc = BukkitAdapter.adapt(block.getLocation());
RegionContainer container = platform.getRegionContainer();
RegionQuery query = container.createQuery();

return query.testState(loc, localPlayer, Flags.BUILD);
return query.testState(loc, wgPlayer, Flags.BUILD);
}
return true;
}
Expand Down
75 changes: 39 additions & 36 deletions src/main/java/com/github/kristianvld/angeltrophies/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.github.kristianvld.angeltrophies;

import com.github.kristianvld.angeltrophies.couch.CouchRole;
import com.github.kristianvld.angeltrophies.couch.CouchUtil;
import com.github.kristianvld.angeltrophies.skin.Skin;
import com.github.kristianvld.angeltrophies.skin.SkinManager;
import com.github.kristianvld.angeltrophies.trophy.Trophy;
import com.github.kristianvld.angeltrophies.trophy.TrophyManager;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
Expand All @@ -16,29 +22,35 @@ public class Main extends JavaPlugin {

private static Main instance;

private StickerManager stickerManager;
private SkinManager skinManager;
private TrophyManager trophyManager;
private CommandHandler cmdHandler;
private External external;

public StickerManager getStickerManager() {
return stickerManager;
public SkinManager getStickerManager() {
return skinManager;
}

@Override
public void onEnable() {
instance = this;
Skin.init(this);
Trophy.init(this);
try {
instance = this;
Skin.init(this);
Trophy.init(this);

cmdHandler = new CommandHandler();
external = new External();
cmdHandler = new CommandHandler();
external = new External();

loadManagers();
loadManagers();
} catch (Exception e) {
getLogger().log(Level.SEVERE, "Error while enabling:", e);
getLogger().severe("An error occurred while enabling AngelTrophies, disabling...");
setEnabled(false);
}
}

public void reload() {
HandlerList.unregisterAll(stickerManager);
HandlerList.unregisterAll(skinManager);
HandlerList.unregisterAll(trophyManager);
getLogger().info("Reloading...");
loadManagers();
Expand Down Expand Up @@ -69,7 +81,7 @@ private void loadManagers() {
try {
id = Integer.parseInt(split[split.length - 1]);
material = String.join("_", Arrays.asList(split).subList(0, split.length - 1));
} catch (NumberFormatException e) {
} catch (NumberFormatException ignored) {
}
material = material.replaceFirst("_", ":");
skins.add(new Skin(Material.matchMaterial(material), id, target, key));
Expand All @@ -78,48 +90,39 @@ private void loadManagers() {
}
}
} catch (Exception e) {
getLogger().log(Level.SEVERE, "Error while parsing skin " + key + ".skin." + source, e);
throw new RuntimeException("Error while parsing skin " + key + ".skin." + source, e);
}
}
} else if (yaml.isConfigurationSection(key + ".trophies")) {
try {
ConfigurationSection t = yaml.getConfigurationSection(key + ".trophies");
boolean floor = false;
boolean floorSmall = false;
double floorOffset = 0.0;
boolean wall = false;
boolean wallSmall = false;
double wallOffset = 0.0;
boolean floorPlaceSlab = false;
float floorRotationResolution = 45;
if (t.isConfigurationSection("floor")) {
floor = true;
floorSmall = t.getBoolean("floor.small", floorSmall);
floorOffset = t.getDouble("floor.offset", floorOffset);
floorPlaceSlab = t.getBoolean("floor.place_slab", floorPlaceSlab);
floorRotationResolution = (float) t.getDouble("floor.rotation_resolution", floorRotationResolution);
}
if (t.isConfigurationSection("wall")) {
wall = true;
wallSmall = t.getBoolean("wall.small", wallSmall);
wallOffset = t.getDouble("wall.offset", wallOffset);
}
boolean floor = t.isConfigurationSection("floor");
boolean floorSmall = t.getBoolean("floor.small", false);
double floorOffset = t.getDouble("floor.offset", 0.0);
boolean wall = t.isConfigurationSection("wall");
boolean wallSmall = t.getBoolean("wall.small", false);
double wallOffset = t.getDouble("wall.offset", 0.0);
boolean floorPlaceSlab = t.getBoolean("floor.place_slab", false);
float floorRotationResolution = (float) t.getDouble("floor.rotation_resolution", 45);
String cGroup = t.getString("floor.couch.group", null);
CouchRole cRole = CouchRole.parse(t.getString("floor.couch.role", null));
if (floor || wall) {
trophies.add(new Trophy(key, floor, floorSmall, floorOffset, wall, wallSmall, wallOffset, floorPlaceSlab, floorRotationResolution));
trophies.add(new Trophy(key, floor, floorSmall, floorOffset, wall, wallSmall, wallOffset, floorPlaceSlab, floorRotationResolution, cGroup, cRole));
}
} catch (Exception e) {
getLogger().log(Level.SEVERE, "Error while parsing trophy " + key + ".trophies", e);
throw new RuntimeException("Error while parsing trophy " + key + ".trophies", e);
}
}
}
} catch (Exception e) {
getLogger().log(Level.SEVERE, "Error while parsing config file " + file.getPath(), e);
throw new RuntimeException("Error while parsing config file " + file.getPath(), e);
}
}
CouchUtil.buildCache(trophies);
getLogger().info("Loaded " + skins.size() + " skins.");
getLogger().info("Loaded " + trophies.size() + " trophies.");

stickerManager = new StickerManager(skins);
skinManager = new SkinManager(skins);
trophyManager = new TrophyManager(trophies);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.kristianvld.angeltrophies.couch;

public enum CouchRole {

Single, RightEnd, LeftEnd, Middle, InnerCorner, OuterCorner;

public static CouchRole parse(String name) {
for (CouchRole role : values()) {
if (role.name().equalsIgnoreCase(name)) {
return role;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.github.kristianvld.angeltrophies.couch;

import com.github.kristianvld.angeltrophies.Main;
import com.github.kristianvld.angeltrophies.trophy.Trophy;
import com.github.kristianvld.angeltrophies.util.Pair;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.util.BlockVector;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class CouchUtil {

private static final Map<Pair<String, CouchRole>, Trophy> trophies = new HashMap<>();

public static void buildCache(Collection<Trophy> trophies) {
Set<String> groups = new HashSet<>();
for (Trophy t : trophies) {
if (t.getCouchRole() != null) {
Pair key = new Pair(t.getCouchGroup(), t.getCouchRole());
if (CouchUtil.trophies.containsKey(key)) {
throw new IllegalArgumentException("The couch part " + t.getCouchGroup() + ":" + t.getCouchRole() + " has been defined twice for the items " + CouchUtil.trophies.get(key).getName() + " and " + t.getName());
}
CouchUtil.trophies.put(key, t);
Main.getInstance().getLogger().info("Loaded couch part " + t.getName() + ", group: " + t.getCouchGroup() + ", role: " + t.getCouchRole());
groups.add(t.getCouchGroup());
}
}

for (String group : groups) {
Trophy single = CouchUtil.trophies.get(new Pair(group, CouchRole.Single));
if (single == null) {
throw new IllegalStateException("Couch group '" + group + "' is missing default role Single!");
}
}
}

public static Trophy getTrophy(String couchGroup, CouchRole couchRole) {
return trophies.get(new Pair(couchGroup, couchRole));
}

private static BlockVector yawToVector(float yaw) {
int x = (int) Math.round(-1 * Math.sin(Math.toRadians(yaw)));
int z = (int) Math.round(1 * Math.cos(Math.toRadians(yaw)));
return new BlockVector(x, 0, z);
}

private static Entity getRelative(Block block, float yaw, String group) {
BlockVector dir = yawToVector(yaw);
Entity trophy = Trophy.getTrophy(block.getRelative(dir.getBlockX(), 0, dir.getBlockZ()));
String id = Trophy.getCouchGroupID(trophy);
if (group.equals(id)) {
return trophy;
}
return null;
}

public static Trophy getTrophy(Trophy trophy, Block block, float yaw) {
if (trophy.getCouchRole() == null) {
return trophy;
}
String group = trophy.getCouchGroup();

yaw = Math.round(yaw / 90) * 90;
Entity back = getRelative(block, yaw, group);
yaw += 90;
Entity left = getRelative(block, yaw, group);
yaw += 90;
Entity forward = getRelative(block, yaw, group);
yaw += 90;
Entity right = getRelative(block, yaw, group);

CouchRole role = CouchRole.Single;

if (left != null) {
if (trophies.containsKey(new Pair(group, CouchRole.LeftEnd))) {
role = CouchRole.LeftEnd;
}
if (forward != null) {
if (trophies.containsKey(new Pair(group, CouchRole.InnerCorner))) {
role = CouchRole.InnerCorner;
}
} else if (right != null) {
if (trophies.containsKey(new Pair(group, CouchRole.Middle))) {
role = CouchRole.Middle;
}
} else if (back != null) {
if (trophies.containsKey(new Pair(group, CouchRole.OuterCorner))) {
role = CouchRole.OuterCorner;
}
}
} else if (right != null) {
if (trophies.containsKey(new Pair(group, CouchRole.LeftEnd))) {
role = CouchRole.RightEnd;
}
}

if (role == CouchRole.Single) {
return trophy;
}
return trophies.get(new Pair(group, role));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.kristianvld.angeltrophies;
package com.github.kristianvld.angeltrophies.skin;

import com.github.kristianvld.angeltrophies.Main;
import io.th0rgal.oraxen.items.OraxenItems;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.github.kristianvld.angeltrophies;
package com.github.kristianvld.angeltrophies.skin;

import com.github.kristianvld.angeltrophies.CommandHandler;
import com.github.kristianvld.angeltrophies.Main;
import com.github.kristianvld.angeltrophies.util.C;
import io.th0rgal.oraxen.items.OraxenItems;
import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand Down Expand Up @@ -36,9 +39,8 @@
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Level;

public class StickerManager implements Listener {
public class SkinManager implements Listener {

private final List<Skin> skins;
private final Set<String> stickerItems = new HashSet<>();
Expand All @@ -49,7 +51,7 @@ public class StickerManager implements Listener {
private final Map<UUID, Long> grinding = new HashMap<>();
private final long GRINDING_TIME = 1000;

public StickerManager(List<Skin> skins) {
public SkinManager(List<Skin> skins) {
this.skins = new ArrayList<>(skins);
for (Skin skin : skins) {
stickerItems.add(skin.getStickerName());
Expand All @@ -70,7 +72,7 @@ public StickerManager(List<Skin> skins) {
}
}
} catch (FileNotFoundException e) {
Main.getInstance().getLogger().log(Level.SEVERE, "Error loading LostSkins file!", e);
throw new RuntimeException("Error loading LostSkins file!", e);
}
}
}
Expand Down
Loading

0 comments on commit 50ab18c

Please sign in to comment.