Skip to content

Commit

Permalink
Implements selection tool, area displays, and /plot create
Browse files Browse the repository at this point in the history
  • Loading branch information
joserobjr committed Aug 9, 2016
1 parent c98843f commit a5293cf
Show file tree
Hide file tree
Showing 34 changed files with 976 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ public Optional<Location> location(EntityPos pos)
return world(pos.world).map(world -> new Location(world, pos.x, pos.y, pos.z, pos.yaw, pos.pitch));
}

@SuppressWarnings("deprecation")
@Override
public Optional<PlayerID> getPlayerId(String name)
{
Expand Down
19 changes: 11 additions & 8 deletions Core/src/main/java/br/com/gamemods/minecity/MineCity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import br.com.gamemods.minecity.api.command.MessageTransformer;
import br.com.gamemods.minecity.api.permission.SimpleFlagHolder;
import br.com.gamemods.minecity.api.world.*;
import br.com.gamemods.minecity.commands.CityCommand;
import br.com.gamemods.minecity.commands.GeneralCommands;
import br.com.gamemods.minecity.commands.GroupCommand;
import br.com.gamemods.minecity.commands.PermissionCommands;
import br.com.gamemods.minecity.commands.*;
import br.com.gamemods.minecity.datasource.api.DataSourceException;
import br.com.gamemods.minecity.datasource.api.IDataSource;
import br.com.gamemods.minecity.datasource.api.unchecked.DBConsumer;
Expand Down Expand Up @@ -76,6 +73,7 @@ public MineCity(@NotNull Server server, @NotNull MineCityConfig config, @Nullabl
commands.registerCommands(new PermissionCommands(this));
commands.registerCommands(new GroupCommand(this));
commands.registerCommands(GeneralCommands.class);
commands.registerCommands(PlotCommand.class);
Inconsistency.setMineCity(this);
}

Expand Down Expand Up @@ -120,16 +118,21 @@ public ChunkPos provideChunk(@NotNull WorldDim world, int x, int z)
}

/**
* @deprecated It's impossible to know if the optional is empty because the chunk is unloaded or because
* the chunk is not claimed
* Gets a chunk claim data from the memory when it's available or fetches from the database when it's not
* @return Empty optional if the chunk is unloaded and not claimed. Non-empty optional may still contains an
* unclaimed chunk that is already loaded.
*/
@Slow
@Deprecated
public Optional<ClaimedChunk> getOrFetchChunkUnchecked(@NotNull ChunkPos pos)
{
return Optional.ofNullable(getChunk(pos).orElseGet((DBSupplier<ClaimedChunk>) () -> dataSource.getCityChunk(pos)));
}

public Optional<ClaimedChunk> getOrFetchChunk(@NotNull ChunkPos pos) throws DataSourceException
{
try
{
return Optional.ofNullable(getChunk(pos).orElseGet((DBSupplier<ClaimedChunk>) () -> dataSource.getCityChunk(pos)));
return getOrFetchChunkUnchecked(pos);
}
catch(UncheckedDataSourceException e)
{
Expand Down
16 changes: 16 additions & 0 deletions Core/src/main/java/br/com/gamemods/minecity/api/MathUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package br.com.gamemods.minecity.api;

public class MathUtil
{
private MathUtil() {}

public static <C extends Comparable<C>, T extends C> T min(T a, T b)
{
return (a.compareTo(b) <= 0)? a : b;
}

public static <C extends Comparable<C>, T extends C> T max(T a, T b)
{
return (a.compareTo(b) >= 0)? a : b;
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
package br.com.gamemods.minecity.api.command;

import br.com.gamemods.minecity.api.Sync;
import br.com.gamemods.minecity.api.world.BlockPos;
import br.com.gamemods.minecity.MineCity;
import br.com.gamemods.minecity.api.world.EntityPos;
import br.com.gamemods.minecity.structure.ClaimedChunk;
import br.com.gamemods.minecity.structure.Inconsistency;

import java.util.List;
import java.util.Optional;

public class CommandEvent
{
public final MineCity mineCity;
public final CommandSender sender;
public final EntityPos position;
public final List<String> path;
public final List<String> args;

public CommandEvent(CommandSender sender, List<String> path, List<String> args)
{
this.mineCity = sender.getServer().getMineCity();
this.sender = sender;
this.path = path;
this.args = args;
position = sender.getPosition();
}

public ClaimedChunk getChunk()
{
return mineCity.getChunk(position.getChunk()).orElseGet(()-> Inconsistency.claim(position.getChunk()));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package br.com.gamemods.minecity.api.command;

import br.com.gamemods.minecity.datasource.api.unchecked.UncheckedDataSourceException;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.InvocationTargetException;

Expand Down Expand Up @@ -42,21 +43,33 @@ default CommandResult<R> run(CommandEvent event)
{
Message message;
if(result.success)
message = new Message("cmd.result.success",
"<msg><blue><![CDATA[MineCity> ]]></blue><gray>${msg}</gray></msg>",
new Object[]{"msg", result.message}
);
message = messageSuccess(result.message);
else
message = new Message("cmd.result.failed",
"<msg><darkred><![CDATA[MineCity> ]]></darkred><red>${msg}</red></msg>",
new Object[]{"msg", result.message}
);
message = messageFailed(result.message);

event.sender.send(message);
}

return result;
}

@NotNull
static Message messageSuccess(@NotNull Message message)
{
return new Message("cmd.result.success",
"<msg><blue><![CDATA[MineCity> ]]></blue><gray>${msg}</gray></msg>",
new Object[]{"msg", message}
);
}

@NotNull
static Message messageFailed(@NotNull Message message)
{
return new Message("cmd.result.failed",
"<msg><darkred><![CDATA[MineCity> ]]></darkred><red>${msg}</red></msg>",
new Object[]{"msg", message}
);
}

CommandResult<R> execute(CommandEvent cmd) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import br.com.gamemods.minecity.api.PlayerID;
import br.com.gamemods.minecity.api.Server;
import br.com.gamemods.minecity.api.unchecked.UFunction;
import br.com.gamemods.minecity.api.world.BlockPos;
import br.com.gamemods.minecity.api.world.Direction;
import br.com.gamemods.minecity.api.world.EntityPos;
import br.com.gamemods.minecity.api.world.MinecraftEntity;
import br.com.gamemods.minecity.api.world.*;
import br.com.gamemods.minecity.structure.Selection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -33,6 +31,17 @@ default String confirm(UFunction<CommandSender, CommandResult<?>> onConfirm)
throw new UnsupportedOperationException();
}

@NotNull
default Selection getSelection(WorldDim world)
{
throw new UnsupportedOperationException();
}

default void giveSelectionTool()
{
throw new UnsupportedOperationException();
}

default CommandResult<CommandResult<?>> confirm(String code) throws Exception
{
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public CommandResult invoke(CommandSender sender, List<String> args)
return result.run(sender);
}

@SuppressWarnings("unused")
public Set<String> getRootCommands()
{
return Collections.unmodifiableSet(tree.keySet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public Message(@NotNull String id, @Nullable String simple)
this(id, simple, (Object[][]) null);
}

public Message(@NotNull String id)
public Message(@NotNull String simple)
{
this(id, id);
this("", simple);
}

@NotNull
Expand Down
26 changes: 23 additions & 3 deletions Core/src/main/java/br/com/gamemods/minecity/api/shape/Cuboid.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public boolean affects(@NotNull ChunkPos chunk)
@Override
public boolean contains(int x, int y, int z)
{
return min.x >= x && max.x <= x
&& min.z >= z && max.z <= z
&& min.y >= y && max.y <= y
return min.x <= x && max.x >= x
&& min.z <= z && max.z >= z
&& min.y <= y && max.y >= y
;
}

Expand Down Expand Up @@ -95,6 +95,26 @@ public int[] next()
};
}

@Override
public boolean overlaps(Shape shape)
{
if(shape instanceof Cuboid)
{
Cuboid a = this;
Cuboid b = (Cuboid) shape;

return a.min.x <= b.max.x
&& a.max.x >= b.min.x
&& a.min.y <= b.max.y
&& a.max.y >= b.min.y
&& a.min.z <= b.max.z
&& a.max.z >= b.min.z
;
}

return Shape.super.overlaps(shape);
}

@Override
public int sizeX()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public boolean affects(@NotNull ChunkPos chunk)
return false;
}

@Override
public boolean overlaps(Shape shape)
{
return false;
}

@Override
public Iterator<int[]> blockIterator()
{
Expand Down
97 changes: 96 additions & 1 deletion Core/src/main/java/br/com/gamemods/minecity/api/shape/Point.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package br.com.gamemods.minecity.api.shape;

import br.com.gamemods.minecity.api.world.Direction;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class Point implements Comparable<Point>
import java.io.Serializable;
import java.util.function.BiFunction;

public class Point implements Serializable, Comparable<Point>
{
private static final long serialVersionUID = 806229182488215329L;
public final int x, y, z;

public Point(int x, int y, int z)
Expand All @@ -13,6 +19,85 @@ public Point(int x, int y, int z)
this.z = z;
}

@NotNull
public <T> Point apply(@Nullable T x, @Nullable T y, @Nullable T z, @NotNull BiFunction<Integer, T, Integer> op)
{
return new Point(op.apply(this.x, x), op.apply(this.y, y), op.apply(this.z, z));
}

@NotNull
public Point apply(@NotNull Direction direction, double multiplier, @NotNull BiFunction<Integer, Double, Integer> op)
{
return apply(direction.x*multiplier, direction.y*multiplier, direction.z*multiplier, op);
}

@NotNull
public Point applyI(@NotNull Direction direction, int multiplier, @NotNull BiFunction<Integer, Integer, Integer> op)
{
return apply(direction.x*multiplier, direction.y*multiplier, direction.z*multiplier, op);
}

@NotNull
public Point add(@NotNull Direction direction, double multiplier)
{
return apply(direction, multiplier, (a,b)-> (int)(a+b));
}

@NotNull
public Point subtract(@NotNull Direction direction, double multiplier)
{
return apply(direction, multiplier, (a,b)-> (int)(a-b));
}


@NotNull
public Point add(@NotNull Direction direction, int multiplier)
{
return applyI(direction, multiplier, (a,b)-> a+b);
}

@NotNull
public Point subtract(@NotNull Direction direction, int multiplier)
{
return applyI(direction, multiplier, (a,b)-> a-b);
}

@NotNull
public Point add(@NotNull Direction direction)
{
return add(direction.x, direction.y, direction.z);
}

@NotNull
public Point subtract(@NotNull Direction direction)
{
return subtract(direction.x, direction.y, direction.z);
}

@NotNull
public Point add(int x, int y, int z)
{
return new Point(this.x+x, this.y+y, this.z+z);
}

@NotNull
public Point subtract(int x, int y, int z)
{
return new Point(this.x-x, this.y-y, this.z-z);
}

@NotNull
public Point multiply(int x, int y, int z)
{
return new Point(this.x*x, this.y*y, this.z*z);
}

@NotNull
public Point divide(int x, int y, int z)
{
return new Point(this.x/x, this.y/y, this.z/z);
}

@Override
public int compareTo(@NotNull Point o)
{
Expand Down Expand Up @@ -47,4 +132,14 @@ public int hashCode()
result = 31*result + z;
return result;
}

@Override
public String toString()
{
return "Point{" +
"x=" + x +
", y=" + y +
", z=" + z +
'}';
}
}
Loading

0 comments on commit a5293cf

Please sign in to comment.