Skip to content

Commit

Permalink
Add allow attack entities permission flag
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed May 30, 2024
1 parent 05c3c91 commit e730429
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static final class Masks {
public static int INTERACT_BLOCK = 1 << 6;
public static int CONTAINERS = 1 << 7;
public static int PVP = 1 << 8;
public static int ATTACK_ENTITY = 1 << 9;
}

/**
Expand Down Expand Up @@ -116,6 +117,14 @@ public void allowAttackPlayer(final boolean allow) {
setAllow(Masks.PVP, allow);
}

public boolean canAttackEntities() {
return checkMask(Masks.ATTACK_ENTITY);
}

public void allowAttackEntities(final boolean allow) {
setAllow(Masks.ATTACK_ENTITY, allow);
}

public Map<String, Boolean> toPermissionsMap() {
HashMap<String, Boolean> permissionsMap = new HashMap<>();

Expand All @@ -128,6 +137,7 @@ public Map<String, Boolean> toPermissionsMap() {
permissionsMap.put("interactBlocks", this.canInteractBlocks());
permissionsMap.put("useContainers", this.canUseContainers());
permissionsMap.put("pvp", this.canAttackPlayer());
permissionsMap.put("attackEntities", this.canAttackEntities());

return permissionsMap;
}
Expand All @@ -148,6 +158,7 @@ public Map<String, Boolean> toPermissionsMap() {
case "interactBlocks" -> chunkPlayerPermissions.allowInteractBlocks(permVal);
case "useContainers" -> chunkPlayerPermissions.allowUseContainers(permVal);
case "pvp" -> chunkPlayerPermissions.allowAttackPlayer(permVal);
case "attackEntities" -> chunkPlayerPermissions.allowAttackEntities(permVal);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,13 @@ public void addPlayer(
joinedPlayers.put(
player,
new FullPlayerData(
player, lastIgn, chunkName, lastOnlineTime, alerts, extraMaxClaims, new ChunkPlayerPermissions(0)));
player,
lastIgn,
chunkName,
lastOnlineTime,
alerts,
extraMaxClaims,
new ChunkPlayerPermissions(0)));
}

@Override
Expand Down Expand Up @@ -410,7 +416,8 @@ private void loadPre0024Data() throws Exception {
player.chunkName,
player.lastOnlineTime,
player.alert,
0, new ChunkPlayerPermissions()));
0,
new ChunkPlayerPermissions()));
// Grant default permissions on all this player's chunks to all players in
// "permitted"
for (DataChunk chunk :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,8 @@ public FullPlayerData[] getFullPlayerData() {
result.getString(3),
result.getLong(4),
result.getBoolean(5),
result.getInt(6), new ChunkPlayerPermissions(0)));
result.getInt(6),
new ChunkPlayerPermissions(0)));
}
} catch (Exception e) {
Utils.err("Failed to retrieve all players data: %s", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,13 @@ public void addPlayer(
int extraMaxClaims) {
addPlayer(
new FullPlayerData(
player, lastIgn, chunkName, lastOnlineTime, alerts, extraMaxClaims, new ChunkPlayerPermissions(0)));
player,
lastIgn,
chunkName,
lastOnlineTime,
alerts,
extraMaxClaims,
new ChunkPlayerPermissions(0)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public static void go() {

// Call migration check methods here.
// Migration method naming scheme:
// migrate_{{MAJOR}}_{{MINOR}}_{{PATCH}}_{{DISCRIMINATOR}}
migrate_0_0_25_1();
// migrate_{{MAJOR}}_{{MINOR}}_{{PATCH}}
migrate_0_0_25();
}

private static void tryCreateTables() {
Expand Down Expand Up @@ -63,9 +63,17 @@ FOREIGN KEY(other_player_uuid) REFERENCES player_data(player_uuid)
""");
}

private static void migrate_0_0_25_1() {
// Whenever a column is added or moved or transformed or whatever, add a
// method here to perform that transformation and call it in initialize_tables.
// I've heard it is really difficult to perform column modification operations, so our best bet
// in that scenario would be to create a temporary table, copy the data to it, delete and
// recreate the table, then copy the data back whilst manually transforming the row data for the
// changed column(s)

private static void migrate_0_0_25() {
if (!columnExists("player_data", "default_chunk_permissions")) {
Q2Sql.executeUpdate("""
Q2Sql.executeUpdate(
"""
ALTER TABLE player_data
ADD default_chunk_permissions INTEGER NOT NULL DEFAULT 0
""");
Expand All @@ -90,8 +98,4 @@ SELECT COUNT(*) FROM pragma_table_info(?) WHERE name=?
}
});
}

// Whenever a column is added or moved or transformed or whatever, add a
// method here to perform that transformation and call it in initialize_tables.

}
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,12 @@ private void onEntityEvent(
@NotNull Player player,
@NotNull Entity entity,
@NotNull EntityAccess.EntityAccessType accessType) {
boolean isPlayerRecipient = entity instanceof Player;

// I don't think interacting with a player really matters
if (isPlayerRecipient && accessType == EntityAccess.EntityAccessType.INTERACT) {
return;
}

// Get the profile for this world
ClaimChunkWorldProfile profile =
Expand All @@ -592,7 +598,11 @@ private void onEntityEvent(
final String permissionNeeded =
entityClass != null && entityClass.equalsIgnoreCase("VEHICLES")
? "interactVehicles"
: (entity instanceof Player ? "pvp" : "interactEntities");
: (isPlayerRecipient
? "pvp"
: (accessType == EntityAccess.EntityAccessType.DAMAGE
? "attackEntities"
: "interactEntities"));

final boolean isOwner = (chunkOwner != null && chunkOwner.equals(ply));
final boolean isOwnerOrAccess =
Expand Down Expand Up @@ -667,8 +677,12 @@ && shouldProtectOwnerChunks(
cancel.run();

// Send cancellation message
final String ownerName =
String ownerName =
claimChunk.getPlayerHandler().getChunkName(neighborOwner);
ownerName =
ownerName == null
? claimChunk.getMessages().unknownChunkOwner
: ownerName;
Utils.toPlayer(
player,
// TODO: FIX THIS METHOD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ public final class V2JsonMessages {
public String argRedstone = "redstone";
public String argInteractVehicles = "interactVehicles";
public String argInteractEntities = "interactEntities";
public String argAttackEntities = "attackEntities";
public String argInteractBlocks = "interactBlocks";
public String argUseContainers = "useContainers";
public String argPvp = "pvp";
Expand All @@ -263,6 +264,7 @@ public final class V2JsonMessages {
argRedstone,
argInteractVehicles,
argInteractEntities,
argAttackEntities,
argInteractBlocks,
argUseContainers,
argPvp,
Expand All @@ -272,7 +274,7 @@ public final class V2JsonMessages {
// Argument types
public String argTypeBoolTrue = "true";
public String argTypeBoolFalse = "false";
public String argTypePlayer = "Player";
// public String argTypePlayer = "Player";

// PlaceholderAPI
public String placeholderApiUnclaimedChunkOwner = "nobody";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public FullPlayerData(
String chunkName,
long lastOnlineTime,
boolean alert,
int extraMaxClaims, ChunkPlayerPermissions defaultChunkPermissions) {
int extraMaxClaims,
ChunkPlayerPermissions defaultChunkPermissions) {
this.player = player;
this.lastIgn = lastIgn;
this.chunkName = chunkName;
Expand All @@ -38,7 +39,8 @@ public FullPlayerData(SqlDataPlayer player) {
player.chunkName,
player.lastOnlineTime,
player.alert,
player.extraMaxClaims, new ChunkPlayerPermissions(player.defaultChunkPermissions));
player.extraMaxClaims,
new ChunkPlayerPermissions(player.defaultChunkPermissions));
}

private FullPlayerData(FullPlayerData clone) {
Expand All @@ -48,7 +50,8 @@ private FullPlayerData(FullPlayerData clone) {
clone.chunkName,
clone.lastOnlineTime,
clone.alert,
clone.extraMaxClaims, clone.defaultChunkPermissions);
clone.extraMaxClaims,
clone.defaultChunkPermissions);
}

public SimplePlayerData toSimplePlayer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public Collection<SimplePlayerData> getJoinedPlayers() {
public List<String> getJoinedPlayersFromName(String start) {
List<String> out = new ArrayList<>();
for (SimplePlayerData ply : dataHandler.getPlayers()) {
if (ply.lastIgn() != null && ply.lastIgn().toLowerCase().startsWith(start.toLowerCase())) {
if (ply.lastIgn() != null
&& ply.lastIgn().toLowerCase().startsWith(start.toLowerCase())) {
out.add(ply.lastIgn());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ public boolean equals(Object o) {
&& player.equals(that.player)
&& lastIgn.equals(that.lastIgn);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cjburkey.claimchunk.smartcommand.sub.ply;

import com.cjburkey.claimchunk.ClaimChunk;
import com.cjburkey.claimchunk.chunk.ChunkPlayerPermissions;
import com.cjburkey.claimchunk.smartcommand.CCSubCommand;

import de.goldmensch.commanddispatcher.Executor;
Expand All @@ -20,18 +21,7 @@
public class AccessCmd extends CCSubCommand {

private static final String[] nonPlayerArguments =
new String[] {
"break",
"place",
"doors",
"redstone",
"interactVehicles",
"interactEntities",
"interactBlocks",
"useContainers",
"pvp",
"allChunks"
};
new ChunkPlayerPermissions().toPermissionsMap().keySet().toArray(new String[0]);

public AccessCmd(ClaimChunk claimChunk) {
// TODO: CREATE `/chunk admin access <PLY>` to allow listing from
Expand All @@ -54,6 +44,7 @@ public CCArg[] getPermittedArguments() {
new CCArg(claimChunk.getMessages().argRedstone, CCAutoComplete.PERMISSION),
new CCArg(claimChunk.getMessages().argInteractVehicles, CCAutoComplete.PERMISSION),
new CCArg(claimChunk.getMessages().argInteractEntities, CCAutoComplete.PERMISSION),
new CCArg(claimChunk.getMessages().argAttackEntities, CCAutoComplete.PERMISSION),
new CCArg(claimChunk.getMessages().argInteractBlocks, CCAutoComplete.PERMISSION),
new CCArg(claimChunk.getMessages().argUseContainers, CCAutoComplete.PERMISSION),
new CCArg(claimChunk.getMessages().argPvp, CCAutoComplete.PERMISSION),
Expand Down
Loading

0 comments on commit e730429

Please sign in to comment.