Skip to content

Instantly share code, notes, and snippets.

@kylepls
Created October 24, 2018 18:32
Show Gist options
  • Save kylepls/0c3f97062035bd0c4fea3f08c30eb399 to your computer and use it in GitHub Desktop.
Save kylepls/0c3f97062035bd0c4fea3f08c30eb399 to your computer and use it in GitHub Desktop.
package in.kyle.plotz2.obj.plot;
import in.kyle.plotz2.Plotz;
import in.kyle.plotz2.config.configs.PlotWorldConfig;
import in.kyle.plotz2.eum.PlotCorner;
import in.kyle.plotz2.obj.player.PlotLocation;
import in.kyle.plotz2.obj.player.PlotPlayer;
import in.kyle.plotz2.world.WorldAdapter;
import lombok.AllArgsConstructor;
/**
* Created by Kyle on Jul 2, 2015
*/
@AllArgsConstructor
public class Calculator {
private final PlotWorld plotWorld;
private final WorldAdapter worldAdapter;
private final Plotz plotz;
protected Plot getPlot(PlotPlayer plotPlayer) {
PlotLocation plotLocation = getPlotLocation(plotPlayer);
return getPlot(plotLocation);
}
protected Plot getPlot(PlotWorldLocation plotWorldLocation) {
PlotLocation plotLocation = getPlotLocation(plotWorldLocation);
return getPlot(plotLocation);
}
protected Plot getPlot(PlotLocation plotLocation) {
if (!plotLocation.isNil()) {
return plotLocation.getPlot();
} else {
return null;
}
}
protected PlotLocation getPlotLocation(PlotPlayer plotPlayer) {
return getPlotLocation(plotPlayer.getLocation(plotz));
}
public PlotLocation getPlotLocation(PlotWorldLocation location) {
return getPlotLocation(location.getX(), location.getZ());
}
protected PlotLocation getPlotLocation(double xCord, double zCord) {
PlotWorldConfig configuration = plotWorld.getConfiguration();
int sx = configuration.getPlotXSize();
int sz = configuration.getPlotZSize();
int path = configuration.getPathSize() + 2;
int xx = (sx + path);// pxsize + path
int zz = (sz + path);
int px = (int) (xCord - configuration.getPlotXOffset());
int pz = (int) (zCord - configuration.getPlotZOffset());
int x = px / xx;
int z = pz / zz;
int dx = px % xx;
int dz = pz % zz;
if (dx < 0) {
dx = xx - Math.abs(dx);
x--;
}
if (dz < 0) {
dz = zz - Math.abs(dz);
z--;
}
dx--;
dz--;
if (dx < 0 || dz < 0 || dx >= sx || dz >= sz) {
return new NullPlotLocation();
}
return new NonNullPlotLocation(plotWorld, x, z);
}
protected boolean isOnPlot(PlotPlayer plotPlayer) {
return getPlotLocation(plotPlayer) != null;
}
protected PlotWorldLocation getCornerLocation(PlotLocation plotLocation, PlotCorner corner) {
PlotWorldConfig configuration = plotLocation.getPlotWorld().getConfiguration();
int pathSize = configuration.getPathSize();
int xx = (configuration.getPlotXSize() * plotLocation.getX()) + ((pathSize + 2) * plotLocation.getX()) + 1 + configuration
.getPlotXOffset();
int zz = (configuration.getPlotZSize() * plotLocation.getZ()) + ((pathSize + 2) * plotLocation.getZ()) + 1 + configuration
.getPlotZOffset();
xx = corner.modX(xx, configuration.getPlotXSize());
zz = corner.modZ(zz, configuration.getPlotZSize());
return new PlotWorldLocation(plotLocation.getPlotWorld(), xx, configuration.getPlotHeight() - 1, zz);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment