[go: nahoru, domu]

Skip to content

Commit

Permalink
Codecov update config (#1638)
Browse files Browse the repository at this point in the history
* style(Code Coverage JarStart):

* perf(updated code coverage allows for more things)

* codecov(test1 AdventureSettings.java)

* codecov(update config)

* codecov(fix AdventureSettings.Type.DOORS_AND_SWITCHES)

* fix(comment): code comments only in English, which is more practical
  • Loading branch information
AzaleeX committed Jul 9, 2024
1 parent 75adada commit 7b9fbe3
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 55 deletions.
16 changes: 16 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
comment:
layout: "reach, diff, flags, files"
behavior: default
require_changes: false

parsers:
gcov:
branch_detection:
conditional: yes
loop: yes
method: yes
macro: yes

coverage:
precision: 2
round: down
range: "70...100"
status:
project:
default:
Expand Down
114 changes: 68 additions & 46 deletions src/main/java/cn/nukkit/AdventureSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.IntTag;
import cn.nukkit.nbt.tag.Tag;
import cn.nukkit.network.protocol.RequestPermissionsPacket;
import cn.nukkit.network.protocol.UpdateAbilitiesPacket;
import cn.nukkit.network.protocol.UpdateAdventureSettingsPacket;
Expand All @@ -20,7 +21,8 @@
import java.util.Map;

/**
* @author MagicDroidX (Nukkit Project)
* AdventureSettings class for managing player abilities and permissions.
* Author: MagicDroidX (Nukkit Project)
*/
public class AdventureSettings implements Cloneable {

Expand All @@ -29,17 +31,22 @@ public class AdventureSettings implements Cloneable {
public static final int PERMISSION_HOST = 2;
public static final int PERMISSION_AUTOMATION = 3;
public static final int PERMISSION_ADMIN = 4;

public static final String KEY_ABILITIES = "Abilities";
public static final String KEY_PLAYER_PERMISSION = "PlayerPermission";
public static final String KEY_COMMAND_PERMISSION = "CommandPermission";

private static final Map<PlayerAbility, Type> ability2TypeMap = new HashMap<>();

private final Map<Type, Boolean> values = new EnumMap<>(Type.class);

@Getter
private PlayerPermission playerPermission;

@Getter
@Setter
private CommandPermission commandPermission;

private Player player;

public AdventureSettings(Player player) {
Expand Down Expand Up @@ -70,29 +77,39 @@ public void init(@Nullable CompoundTag nbt) {

commandPermission = player.isOp() ? CommandPermission.OPERATOR : CommandPermission.NORMAL;
playerPermission = player.isOp() ? PlayerPermission.OPERATOR : PlayerPermission.MEMBER;
} else readNBT(nbt);
} else {
readNBT(nbt);
}

//离线时被deop
if (playerPermission == PlayerPermission.OPERATOR && !player.isOp()) onOpChange(false);
//离线时被op
if (playerPermission != PlayerPermission.OPERATOR && player.isOp()) onOpChange(true);
//Offline deop
if (playerPermission == PlayerPermission.OPERATOR && !player.isOp()) {
onOpChange(false);
}
//Offline by op
if (playerPermission != PlayerPermission.OPERATOR && player.isOp()) {
onOpChange(true);
}
}

public AdventureSettings clone(Player newPlayer) {
@Override
public AdventureSettings clone() {
try {
AdventureSettings settings = (AdventureSettings) super.clone();
settings.values.putAll(this.values);
settings.player = newPlayer;
settings.playerPermission = playerPermission;
settings.commandPermission = commandPermission;
settings.player = this.player;
settings.playerPermission = this.playerPermission;
settings.commandPermission = this.commandPermission;
return settings;
} catch (CloneNotSupportedException e) {
return null;
throw new AssertionError(); // This should never happen.
}
}

public AdventureSettings set(PlayerAbility ability, boolean value) {
this.values.put(ability2TypeMap.get(ability), value);
Type type = ability2TypeMap.get(ability);
if (type != null) {
this.values.put(type, value);
}
return this;
}

Expand All @@ -102,49 +119,51 @@ public AdventureSettings set(Type type, boolean value) {
}

public boolean get(PlayerAbility ability) {
var type = ability2TypeMap.get(ability);
Boolean value = this.values.get(type);

return value == null ? type.getDefaultValue() : value;
Type type = ability2TypeMap.get(ability);
if (type == null) {
throw new IllegalArgumentException("Unknown ability: " + ability);
}
return this.values.getOrDefault(type, type.getDefaultValue());
}

public boolean get(Type type) {
Boolean value = this.values.get(type);

return value == null ? type.getDefaultValue() : value;
return this.values.getOrDefault(type, type.getDefaultValue());
}

public void update() {
//向所有玩家发送以使他们能看到彼此的权限
//Permission to send to all players so they can see each other
var players = new HashSet<>(player.getServer().getOnlinePlayers().values());
//确保会发向自己(eg:玩家进服时在线玩家里没有此玩家)
//Make sure it will be sent to yourself (eg: there is no such player among the online players when the player enters the server)
Collection<Player> players = new HashSet<>(player.getServer().getOnlinePlayers().values());
players.add(this.player);
this.sendAbilities(players);
this.updateAdventureSettings();
sendAbilities(players);
updateAdventureSettings();
}


/**
* 当玩家OP身份变动时此方法将被调用
* 注意此方法并不会向客户端发包刷新权限信息,你需要手动调用update()方法刷新
*
* @param op 是否是OP
* This method will be called when the player's OP status changes.
* Note that this method does not send a packet to the client to refresh the privilege information, you need to manually call the update() method to do so
* @param op is OP or not
*/
public void onOpChange(boolean op) {
if (op) {
for (PlayerAbility controllableAbility : RequestPermissionsPacket.CONTROLLABLE_ABILITIES)
for (PlayerAbility controllableAbility : RequestPermissionsPacket.CONTROLLABLE_ABILITIES) {
set(controllableAbility, true);
}
}
//设置op特有属性
//Set op-specific attributes
set(Type.OPERATOR, op);
set(Type.TELEPORT, op);

commandPermission = op ? CommandPermission.OPERATOR : CommandPermission.NORMAL;
//不要覆盖自定义/访客状态
if (op && playerPermission != PlayerPermission.OPERATOR) playerPermission = PlayerPermission.OPERATOR;
if (!op && playerPermission == PlayerPermission.OPERATOR) playerPermission = PlayerPermission.MEMBER;

//Don't override customization/guest status
if (op && playerPermission != PlayerPermission.OPERATOR) {
playerPermission = PlayerPermission.OPERATOR;
}
if (!op && playerPermission == PlayerPermission.OPERATOR) {
playerPermission = PlayerPermission.MEMBER;
}
}

public void sendAbilities(Collection<Player> players) {
Expand All @@ -158,16 +177,15 @@ public void sendAbilities(Collection<Player> players) {
layer.getAbilitiesSet().addAll(PlayerAbility.VALUES);

for (Type type : Type.values()) {
if (type.isAbility() && this.get(type)) {
if (type.isAbility() && get(type)) {
layer.getAbilityValues().add(type.getAbility());
}
}

if (player.isCreative()) { // Make sure player can interact with creative menu
if (player.isCreative()) {
layer.getAbilityValues().add(PlayerAbility.INSTABUILD);
}

// Because we send speed
layer.getAbilityValues().add(PlayerAbility.WALK_SPEED);
layer.getAbilityValues().add(PlayerAbility.FLY_SPEED);

Expand All @@ -180,11 +198,11 @@ public void sendAbilities(Collection<Player> players) {
}

/**
* 保存权限到nbt
* Save permissions to nbt
*/
public void saveNBT() {
var nbt = player.namedTag;
var abilityTag = new CompoundTag();
CompoundTag nbt = player.namedTag;
CompoundTag abilityTag = new CompoundTag();
values.forEach((type, bool) -> {
abilityTag.put(type.name(), new IntTag(bool ? 1 : 0));
});
Expand All @@ -194,12 +212,14 @@ public void saveNBT() {
}

/**
* 从nbt读取权限数据
* Read permission data from nbt
*/
public void readNBT(CompoundTag nbt) {
var abilityTag = nbt.getCompound(KEY_ABILITIES);
for (var e : abilityTag.getTags().entrySet()) {
set(Type.valueOf(e.getKey()), ((IntTag) e.getValue()).getData() == 1);
CompoundTag abilityTag = nbt.getCompound(KEY_ABILITIES);
for (Map.Entry<String, Tag> e : abilityTag.getTags().entrySet()) {
if (e.getValue() instanceof IntTag) {
set(Type.valueOf(e.getKey()), ((IntTag) e.getValue()).getData() == 1);
}
}
playerPermission = PlayerPermission.valueOf(nbt.getString(KEY_PLAYER_PERMISSION));
commandPermission = CommandPermission.valueOf(nbt.getString(KEY_COMMAND_PERMISSION));
Expand Down Expand Up @@ -228,7 +248,7 @@ public enum Type {
FLYING(PlayerAbility.FLYING, false),
MUTED(PlayerAbility.MUTED, false),
MINE(PlayerAbility.MINE, true),
DOORS_AND_SWITCHED(PlayerAbility.DOORS_AND_SWITCHES, true),
DOORS_AND_SWITCHES(PlayerAbility.DOORS_AND_SWITCHES, true),
OPEN_CONTAINERS(PlayerAbility.OPEN_CONTAINERS, true),
ATTACK_PLAYERS(PlayerAbility.ATTACK_PLAYERS, true),
ATTACK_MOBS(PlayerAbility.ATTACK_MOBS, true),
Expand All @@ -248,7 +268,9 @@ public enum Type {
Type(PlayerAbility ability, boolean defaultValue) {
this.ability = ability;
this.defaultValue = defaultValue;
if (this.ability != null) ability2TypeMap.put(this.ability, this);
if (this.ability != null) {
ability2TypeMap.put(this.ability, this);
}
}

public boolean getDefaultValue() {
Expand All @@ -263,4 +285,4 @@ public boolean isAbility() {
return this.ability != null;
}
}
}
}
9 changes: 5 additions & 4 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,8 @@ public void processLogin() {
nbt.putInt("playerGameType", this.gamemode);
}

this.adventureSettings = new AdventureSettings(this, nbt);
this.adventureSettings = new AdventureSettings(this);
this.adventureSettings.init(nbt);

Level level;
if ((level = this.server.getLevelByName(nbt.getString("Level"))) == null) {
Expand Down Expand Up @@ -1600,7 +1601,7 @@ public AdventureSettings getAdventureSettings() {
* @param adventureSettings 玩家权限设置<br>player permissions settings
*/
public void setAdventureSettings(AdventureSettings adventureSettings) {
this.adventureSettings = adventureSettings.clone(this);
this.adventureSettings = adventureSettings.clone();
this.adventureSettings.update();
}

Expand Down Expand Up @@ -1650,7 +1651,7 @@ public void setAllowInteract(boolean value) {
*/
public void setAllowInteract(boolean value, boolean containers) {
this.getAdventureSettings().set(Type.WORLD_IMMUTABLE, !value);
this.getAdventureSettings().set(Type.DOORS_AND_SWITCHED, value);
this.getAdventureSettings().set(Type.DOORS_AND_SWITCHES, value);
this.getAdventureSettings().set(Type.OPEN_CONTAINERS, containers);
this.getAdventureSettings().update();
}
Expand Down Expand Up @@ -2362,7 +2363,7 @@ public boolean setGamemode(int gamemode, boolean serverSide, AdventureSettings n
}

if (newSettings == null) {
newSettings = this.getAdventureSettings().clone(this);
newSettings = this.getAdventureSettings().clone();
newSettings.set(Type.WORLD_IMMUTABLE, (gamemode & 0x02) > 0);
newSettings.set(Type.BUILD, (gamemode & 0x02) <= 0);
newSettings.set(Type.WORLD_BUILDER, (gamemode & 0x02) <= 0);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/block/BlockButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public boolean canBeActivated() {
public boolean onActivate(@NotNull Item item, Player player, BlockFace blockFace, float fx, float fy, float fz) {
if (player == null) {
return false;
} else if (!player.getAdventureSettings().get(AdventureSettings.Type.DOORS_AND_SWITCHED))
} else if (!player.getAdventureSettings().get(AdventureSettings.Type.DOORS_AND_SWITCHES))
return false;
Item itemInHand = player.getInventory().getItemInHand();
if (player.isSneaking() && !(itemInHand.isTool() || itemInHand.isNull())) return false;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/block/BlockDoor.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public void playCloseSound() {
}

public boolean toggle(Player player) {
if (!player.getAdventureSettings().get(AdventureSettings.Type.DOORS_AND_SWITCHED))
if (!player.getAdventureSettings().get(AdventureSettings.Type.DOORS_AND_SWITCHES))
return false;
return this.setOpen(player, !this.isOpen());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/block/BlockFenceGate.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public boolean onActivate(@NotNull Item item, Player player, BlockFace blockFace
}

public boolean toggle(Player player) {
if (!player.getAdventureSettings().get(AdventureSettings.Type.DOORS_AND_SWITCHED))
if (!player.getAdventureSettings().get(AdventureSettings.Type.DOORS_AND_SWITCHES))
return false;
return this.setOpen(player, !this.isOpen());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/block/BlockLever.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void setLeverOrientation(@Nullable LeverDirection value) {

@Override
public boolean onActivate(@NotNull Item item, Player player, BlockFace blockFace, float fx, float fy, float fz) {
if (player!=null && !player.getAdventureSettings().get(AdventureSettings.Type.DOORS_AND_SWITCHED)) return false;
if (player!=null && !player.getAdventureSettings().get(AdventureSettings.Type.DOORS_AND_SWITCHES)) return false;
if(isNotActivate(player)) return false;
this.level.getServer().getPluginManager().callEvent(new BlockRedstoneEvent(this, isPowerOn() ? 15 : 0, isPowerOn() ? 0 : 15));
setPowerOn(!isPowerOn());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/block/BlockTrapdoor.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public boolean onActivate(@NotNull Item item, Player player, BlockFace blockFace
}

public boolean toggle(Player player) {
if (!player.getAdventureSettings().get(AdventureSettings.Type.DOORS_AND_SWITCHED))
if (!player.getAdventureSettings().get(AdventureSettings.Type.DOORS_AND_SWITCHES))
return false;
return this.setOpen(player, !this.isOpen());
}
Expand Down

0 comments on commit 7b9fbe3

Please sign in to comment.