Skip to content

Commit

Permalink
Use ImplicitPosition in more places
Browse files Browse the repository at this point in the history
Refactor some method names
Also added TriBoolean.Mapper thingy
  • Loading branch information
imDaniX committed Aug 14, 2023
1 parent 7c1587d commit 4abd464
Show file tree
Hide file tree
Showing 22 changed files with 346 additions and 304 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,7 @@ public void onSneak(PlayerToggleSneakEvent event) {
public void onPlayerQuit(PlayerQuitEvent event) {
TemporaryOp.removeOp(event.getPlayer());
Optional<Variables> optVars = triggerQuit(event);
if (optVars.isPresent()){
optVars.get().getChanged(QuitActivator.Context.QUIT_MESSAGE).ifPresent(event::setQuitMessage);
}
optVars.flatMap(variables -> variables.getChanged(QuitActivator.Context.QUIT_MESSAGE)).ifPresent(event::setQuitMessage);
MoveListener.removeLocation(event.getPlayer());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static int sizeTpLoc() {

public static boolean addTpLoc(String id, Location loc) {
if (Utils.isStringEmpty(id)) return false;
tports.put(id, RealPosition.of(loc));
tports.put(id, RealPosition.byLocation(loc));
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.jetbrains.annotations.NotNull;

@Aliased.Names("JUMP")
@Deprecated
public class VelocityJumpAction implements Action {
@Override
public boolean proceed(@NotNull Environment env, @NotNull String paramsStr) {
Expand All @@ -45,7 +46,7 @@ public boolean proceed(@NotNull Environment env, @NotNull String paramsStr) {
Location loc = LocationUtils.parseCoordinates(locStr);
if (loc == null) return false;
int jumpHeight = params.getInteger("jump", 5);
Vector velocity = LocationUtils.calculateVelocity(player.getLocation(), loc, jumpHeight);
Vector velocity = calculateVelocity(player.getLocation(), loc, jumpHeight);
player.setVelocity(velocity);
return false;
}
Expand All @@ -59,4 +60,48 @@ public boolean proceed(@NotNull Environment env, @NotNull String paramsStr) {
public boolean requiresPlayer() {
return true;
}

private static Vector calculateVelocity(Location locFrom, Location locTo, int heightGain) {
if (!locFrom.getWorld().equals(locTo.getWorld())) return new Vector(0, 0, 0);
// Gravity of a potion
double gravity = 0.18; //0.115;
Vector from = locFrom.toVector();
Vector to = locTo.toVector();


// Block locations
int endGain = to.getBlockY() - from.getBlockY();
double horizDist = from.distance(to);

// Height gain

//double maxGain = gain > (endGain + gain) ? gain : (endGain + gain);
double maxGain = Math.max(heightGain, endGain + heightGain);

// Solve quadratic equation for velocity
double a = -horizDist * horizDist / (4 * maxGain);
double c = -endGain;

double slope = -horizDist / (2 * a) - Math.sqrt(horizDist * horizDist - 4 * a * c) / (2 * a);

// Vertical velocity
double vy = Math.sqrt(maxGain * gravity);

// Horizontal velocity
double vh = vy / slope;

// Calculate horizontal direction
int dx = to.getBlockX() - from.getBlockX();
int dz = to.getBlockZ() - from.getBlockZ();
double mag = Math.sqrt(dx * dx + dz * dz);
double dirx = dx / mag;
double dirz = dz / mag;

// Horizontal velocity components
double vx = vh * dirx;
double vz = vh * dirz;

return new Vector(vx, vy, vz);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import fun.reactions.model.activators.Activator;
import fun.reactions.model.activators.Locatable;
import fun.reactions.model.environment.Variable;
import fun.reactions.util.Utils;
import fun.reactions.util.item.ItemUtils;
import fun.reactions.util.location.ImplicitPosition;
import fun.reactions.util.location.LocationUtils;
import fun.reactions.util.parameter.Parameters;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
Expand All @@ -28,25 +27,24 @@
public class BlockBreakActivator extends Activator implements Locatable {

private final Material blockType;
// TODO: VirtualLocation
private final String blockLocation;
private final ImplicitPosition pos;

private BlockBreakActivator(Logic base, Material block, String location) {
private BlockBreakActivator(Logic base, Material block, ImplicitPosition pos) {
super(base);
this.blockType = block;
this.blockLocation = location;
this.pos = pos;
}

public static BlockBreakActivator create(Logic base, Parameters param) {
Material block = ItemUtils.getMaterial(param.getString("block"));
String loc = param.getString("loc");
return new BlockBreakActivator(base, block, loc);
ImplicitPosition pos = param.get("loc", ImplicitPosition::byString);
return new BlockBreakActivator(base, block, pos);
}

public static BlockBreakActivator load(Logic base, ConfigurationSection cfg) {
Material block = ItemUtils.getMaterial(cfg.getString("block", ""));
String loc = cfg.getString("loc");
return new BlockBreakActivator(base, block, loc);
ImplicitPosition pos = ImplicitPosition.byString(cfg.getString("loc"));
return new BlockBreakActivator(base, block, pos);
}

@Override
Expand All @@ -59,36 +57,25 @@ public boolean checkContext(@NotNull ActivationContext context) {

private boolean isActivatorBlock(Block block) {
if (this.blockType != null && blockType != block.getType()) return false;
if (Utils.isStringEmpty(blockLocation)) return true;
return this.isLocatedAt(block.getLocation());
}

public boolean isLocatedAt(Location l) {
if (Utils.isStringEmpty(blockLocation)) return false;
Location loc = LocationUtils.parseLocation(this.blockLocation, null);
if (loc == null) return false;
return l.getWorld().equals(loc.getWorld()) &&
l.getBlockX() == loc.getBlockX() &&
l.getBlockY() == loc.getBlockY() &&
l.getBlockZ() == loc.getBlockZ();
return pos.isValidAt(block.getLocation());
}

@Override
public boolean isLocatedAt(@NotNull World world, int x, int y, int z) {
return isLocatedAt(new Location(world, x, y, z));
return pos.isValidAt(world.getName(), x, y, z);
}

@Override
public void saveOptions(@NotNull ConfigurationSection cfg) {
cfg.set("block", blockType == null ? null : blockType.name());
cfg.set("location", Utils.isStringEmpty(blockLocation) ? null : blockLocation);
cfg.set("location", pos == ImplicitPosition.EVERYWHERE ? null : pos);
}

@Override
public String toString() {
String sb = super.toString() + " (" +
"block:" + (blockType == null ? "-" : blockType) +
"; loc:" + (blockLocation == null ? "-" : blockLocation) +
"; loc:" + (pos == ImplicitPosition.EVERYWHERE ? "-" : pos) +
")";
return sb;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private ButtonActivator(Logic base, ImplicitPosition pos) {

public static ButtonActivator create(Logic base, Parameters p) {
if (!(p instanceof BlockParameters param)) return null;
return new ButtonActivator(base, ImplicitPosition.of(param.getBlock().getLocation()));
return new ButtonActivator(base, ImplicitPosition.byLocation(param.getBlock().getLocation()));
}

public static ButtonActivator load(Logic base, ConfigurationSection cfg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*
*/


package fun.reactions.module.basics.activators;

import fun.reactions.model.Logic;
Expand All @@ -29,108 +28,85 @@
import fun.reactions.model.activators.Locatable;
import fun.reactions.model.environment.Variable;
import fun.reactions.util.BlockUtils;
import fun.reactions.util.Utils;
import fun.reactions.util.enums.TriBoolean;
import fun.reactions.util.location.ImplicitPosition;
import fun.reactions.util.location.LocationUtils;
import fun.reactions.util.parameter.BlockParameters;
import fun.reactions.util.parameter.Parameters;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Locale;
import java.util.Map;

// TODO Use custom enum or TriBoolean
public class DoorActivator extends Activator implements Locatable {
private final String state; //open, close
//координаты нижнего блока двери
private final String world;
private final int x;
private final int y;
private final int z;

private DoorActivator(Logic base, String state, String world, int x, int y, int z) {
private static final TriBoolean.Mapper STATE_MAPPER = new TriBoolean.Mapper.Builder()
.trueMain("OPEN").addTrueVariants("OPENED")
.falseMain("CLOSE").addFalseVariants("CLOSED")
.build();

private final TriBoolean state;
private final ImplicitPosition lowerPos;

private DoorActivator(Logic base, TriBoolean state, ImplicitPosition lowerPos) {
super(base);
this.state = state;
this.world = world;
this.x = x;
this.y = y;
this.z = z;
this.lowerPos = lowerPos;
}

public static DoorActivator create(Logic base, Parameters p) {
if (!(p instanceof BlockParameters param)) return null;
Block targetBlock = param.getBlock();
if (targetBlock == null || BlockUtils.isOpenable(targetBlock)) {
String state = param.getString("state", "ANY");
if (!(state.equalsIgnoreCase("open") || state.equalsIgnoreCase("close"))) state = "ANY";
String world = targetBlock.getWorld().getName();
int x = targetBlock.getX();
int y = targetBlock.getY();
int z = targetBlock.getZ();
return new DoorActivator(base, state, world, x, y, z);
} else return null;
public static DoorActivator create(Logic base, Parameters params) {
Block targetBlock = params instanceof BlockParameters blockParams ? blockParams.getBlock() : null;
TriBoolean state = params.get(params.findKey(Parameters.ORIGIN, "state"), STATE_MAPPER::byString);
if (targetBlock != null && targetBlock.getType() == Material.LEVER) {
return new DoorActivator(base, state, ImplicitPosition.byLocation(targetBlock.getLocation()));
} else {
return new DoorActivator(base, state, params.getSafe("location", ImplicitPosition::byString));
}
}

public static DoorActivator load(Logic base, ConfigurationSection cfg) {
String state = cfg.getString("state", "ANY");
if (!(state.equalsIgnoreCase("open") || state.equalsIgnoreCase("close"))) state = "ANY";
String world = cfg.getString("world");
int x = cfg.getInt("x");
int y = cfg.getInt("y");
int z = cfg.getInt("z");
return new DoorActivator(base, state, world, x, y, z);
ImplicitPosition pos;
if (cfg.isString("location")) {
pos = ImplicitPosition.byString(cfg.getString("location"));
} else {
pos = ImplicitPosition.of(
cfg.getString("world"),
cfg.getInt("x"),
cfg.getInt("y"),
cfg.getInt("z")
);
}
TriBoolean state = STATE_MAPPER.byString(cfg.getString("state"));
return new DoorActivator(base, state, pos);
}

@Override
public boolean checkContext(@NotNull ActivationContext context) {
Context de = (Context) context;
if (de.doorBlock == null) return false;
if (!isLocatedAt(de.getDoorLocation())) return false;
if (this.state.equalsIgnoreCase("open") && de.isDoorOpened()) return false;
return !this.state.equalsIgnoreCase("close") || (de.isDoorOpened());
}

public boolean isLocatedAt(Location l) {
if (l == null) return false;
if (!world.equals(l.getWorld().getName())) return false;
if (x != l.getBlockX()) return false;
if (y != l.getBlockY()) return false;
return (z == l.getBlockZ());
return state.isValidFor(de.isDoorOpened()) && lowerPos.isValidAt(de.getDoorLocation());
}

@Override
public boolean isLocatedAt(@NotNull World world, int x, int y, int z) {
return this.world.equals(world.getName()) &&
this.x == x &&
this.y == y &&
this.z == z;
return lowerPos.isValidAt(world.getName(), x, y, z);
}

@Override
public void saveOptions(@NotNull ConfigurationSection cfg) {
cfg.set("world", this.world);
cfg.set("x", x);
cfg.set("y", y);
cfg.set("z", z);
cfg.set("state", state);
}

@Override
public boolean isValid() {
return !Utils.isStringEmpty(world);
cfg.set("location", lowerPos.toString());
cfg.set("state", STATE_MAPPER.toString(state));
}

@Override
public String toString() {
String sb = super.toString() + " (" +
world + ", " + x + ", " + y + ", " + z +
"; state:" + this.state.toUpperCase(Locale.ROOT) +
return super.toString() + " (" +
lowerPos + "; " +
"state:" + STATE_MAPPER.toString(state) +
")";
return sb;
}

public static class Context extends ActivationContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static FlightActivator create(Logic base, Parameters param) {
}

public static FlightActivator load(Logic base, ConfigurationSection cfg) {
return new FlightActivator(base, TriBoolean.of(cfg.getString("flight")));
return new FlightActivator(base, TriBoolean.byString(cfg.getString("flight")));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static GodActivator create(Logic base, Parameters param) {
}

public static GodActivator load(Logic base, ConfigurationSection cfg) {
return new GodActivator(base, TriBoolean.of(cfg.getString("god")));
return new GodActivator(base, TriBoolean.byString(cfg.getString("god")));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private ItemClickActivator(Logic base, String item, HandType hand) {

public static ItemClickActivator create(Logic base, Parameters param) {
String item = param.getString("item", param.origin());
HandType hand = param.get("hand", HandType::getByName);
HandType hand = param.getSafe("hand", HandType::getByName);
return new ItemClickActivator(base, item, hand);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static JoinActivator create(Logic base, Parameters param) {
}

public static JoinActivator load(Logic base, ConfigurationSection cfg) {
return new JoinActivator(base, TriBoolean.of(cfg.getString("first-join")));
return new JoinActivator(base, TriBoolean.byString(cfg.getString("first-join")));
}

@Override
Expand Down
Loading

0 comments on commit 4abd464

Please sign in to comment.