Support different field dimensions

This commit is contained in:
Maxopoly
2022-03-14 17:54:38 +01:00
parent f92ae27401
commit aa63ff945b
6 changed files with 94 additions and 41 deletions

View File

@@ -13,3 +13,5 @@
.gradle
build
paper/bin

View File

@@ -15,7 +15,7 @@ subprojects {
apply(plugin = "java-library")
apply(plugin = "maven-publish")
group = "net.cimc.jukealert"
group = "net.civmc.jukealert"
version = "3.0.0-SNAPSHOT"
java {

View File

@@ -1,6 +1,7 @@
package com.untamedears.jukealert.model;
import com.untamedears.jukealert.model.appender.AbstractSnitchAppender;
import com.untamedears.jukealert.model.field.FieldManager;
import com.untamedears.jukealert.model.field.SingleCuboidRangeManager;
import java.util.List;
import java.util.function.Function;
@@ -12,21 +13,21 @@ public class SnitchFactoryType {
private final int id;
private final ItemStack item;
private final String name;
private final int range;
private final Function<Snitch, FieldManager> fieldGenerator;
private final List<Function<Snitch, AbstractSnitchAppender>> appenders;
public SnitchFactoryType(ItemStack item, int range, String name, int id,
public SnitchFactoryType(ItemStack item, Function<Snitch, FieldManager> fieldGenerator, String name, int id,
List<Function<Snitch, AbstractSnitchAppender>> appenders) {
this.item = item;
this.name = name;
this.id = id;
this.range = range;
this.fieldGenerator = fieldGenerator;
this.appenders = appenders;
}
public Snitch create(int snitchID, Location location, String name, int groupID, boolean isNew) {
Snitch snitch = new Snitch(snitchID, location, isNew, groupID, s -> new SingleCuboidRangeManager(range, s),
Snitch snitch = new Snitch(snitchID, location, isNew, groupID, fieldGenerator,
this, name);
for(Function<Snitch, AbstractSnitchAppender> appenderFunc : appenders) {
AbstractSnitchAppender appender = appenderFunc.apply(snitch);

View File

@@ -7,6 +7,10 @@ import com.untamedears.jukealert.model.appender.DormantCullingAppender;
import com.untamedears.jukealert.model.appender.LeverToggleAppender;
import com.untamedears.jukealert.model.appender.ShowOwnerOnDestroyAppender;
import com.untamedears.jukealert.model.appender.SnitchLogAppender;
import com.untamedears.jukealert.model.field.FieldManager;
import com.untamedears.jukealert.model.field.SingleCuboidRangeManager;
import com.untamedears.jukealert.model.field.VariableSizeCuboidRangeManager;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
@@ -61,8 +65,9 @@ public class SnitchTypeManager {
return false;
}
String name = config.getString("name");
if (!config.isInt("range")) {
logger.warning("Snitch type at " + config.getCurrentPath() + " had no range specified");
Function<Snitch, FieldManager> fieldGenerator = getFieldInstanciation(config);
if (fieldGenerator == null) {
logger.warning("Snitch type at " + config.getCurrentPath() + " had no valid field specified");
return false;
}
sb.append("Successfully parsed type ");
@@ -71,9 +76,6 @@ public class SnitchTypeManager {
sb.append(id);
sb.append(", item: ");
sb.append(item.toString());
int range = config.getInt("range");
sb.append(", range: ");
sb.append(range);
sb.append(", appenders: ");
List<Function<Snitch, AbstractSnitchAppender>> appenderInstanciations = new ArrayList<>();
if (config.isConfigurationSection("appender")) {
@@ -101,12 +103,41 @@ public class SnitchTypeManager {
if (appenderInstanciations.isEmpty()) {
logger.warning("Snitch config at " + config.getCurrentPath() + " has no appenders, this is likely not what you intended");
}
SnitchFactoryType configFactory = new SnitchFactoryType(item, range, name, id, appenderInstanciations);
SnitchFactoryType configFactory = new SnitchFactoryType(item, fieldGenerator, name, id, appenderInstanciations);
configFactoriesById.put(configFactory.getID(), configFactory);
configFactoriesByItem.put(configFactory.getItem(), configFactory);
logger.info(sb.toString());
return true;
}
private Function<Snitch, FieldManager> getFieldInstanciation(ConfigurationSection config) {
Logger logger = JukeAlert.getInstance().getLogger();
if (config.isInt("range")) {
int range = config.getInt("range");
return (s -> new SingleCuboidRangeManager(range, s));
}
ConfigurationSection rangeSection = config.getConfigurationSection("range");
if (rangeSection == null) {
logger.warning("Snitch config at " + config.getCurrentPath() + " had no range config");
return null;
}
String type = rangeSection.getString("type", "none");
switch(type) {
case "cube":
int range = rangeSection.getInt("range", 11);
logger.info("Parsed cube FieldManager with range " + range);
return (s -> new SingleCuboidRangeManager(range, s));
case "cuboid":
int width = rangeSection.getInt("width");
int height = rangeSection.getInt("height");
logger.info("Parsed cuboid FieldManager with width " + width + " and height " + height);
return (s -> new VariableSizeCuboidRangeManager(width, height, s));
default:
logger.warning("Unknown range type " + type + " at " + rangeSection.getCurrentPath());
return null;
}
}
/**
* Creates a function which will instanciate the appender based on the

View File

@@ -6,38 +6,10 @@ import com.untamedears.jukealert.model.SnitchQTEntry;
import java.util.Collection;
import org.bukkit.Location;
public class SingleCuboidRangeManager implements FieldManager {
private Snitch snitch;
private SnitchQTEntry entry;
private int range;
public class SingleCuboidRangeManager extends VariableSizeCuboidRangeManager {
public SingleCuboidRangeManager(int range, Snitch snitch) {
entry = new SnitchQTEntry(snitch, snitch.getLocation(), range);
this.snitch = snitch;
this.range = range;
}
@Override
public boolean isInside(Location location) {
int x = snitch.getLocation().getBlockX();
if (location.getBlockX() > (x + range) || location.getBlockX() < (x - range)) {
return false;
}
int y = snitch.getLocation().getBlockY();
if (location.getBlockY() > (y + range) || location.getBlockY() < (y - range)) {
return false;
}
int z = snitch.getLocation().getBlockZ();
if (location.getBlockZ() > (z + range) || location.getBlockZ() < (z - range)) {
return false;
}
return true;
}
@Override
public Collection<SnitchQTEntry> getQTEntries() {
return Lists.asList(entry, new SnitchQTEntry[0]);
super(range, range, snitch);
}
}

View File

@@ -0,0 +1,47 @@
package com.untamedears.jukealert.model.field;
import java.util.Collection;
import org.bukkit.Location;
import com.google.common.collect.Lists;
import com.untamedears.jukealert.model.Snitch;
import com.untamedears.jukealert.model.SnitchQTEntry;
public class VariableSizeCuboidRangeManager implements FieldManager {
private Snitch snitch;
private SnitchQTEntry entry;
private int width;
private int height;
public VariableSizeCuboidRangeManager(int width, int height, Snitch snitch) {
entry = new SnitchQTEntry(snitch, snitch.getLocation(), width);
this.snitch = snitch;
this.width = width;
this.height = height;
}
@Override
public boolean isInside(Location location) {
int x = snitch.getLocation().getBlockX();
if (location.getBlockX() > (x + width) || location.getBlockX() < (x - width)) {
return false;
}
int y = snitch.getLocation().getBlockY();
if (location.getBlockY() > (y + height) || location.getBlockY() < (y - height)) {
return false;
}
int z = snitch.getLocation().getBlockZ();
if (location.getBlockZ() > (z + width) || location.getBlockZ() < (z - width)) {
return false;
}
return true;
}
@Override
public Collection<SnitchQTEntry> getQTEntries() {
return Lists.asList(entry, new SnitchQTEntry[0]);
}
}