优化代码

This commit is contained in:
tanyaofei 2023-11-22 15:51:44 +08:00
parent 18bb7bb48b
commit 05b73e86e6
5 changed files with 26 additions and 11 deletions

View File

@ -1,4 +1,4 @@
package io.github.hello09x.fakeplayer.core.common; package io.github.hello09x.fakeplayer.core.constant;
public interface ConstantPool { public interface ConstantPool {

View File

@ -0,0 +1,13 @@
package io.github.hello09x.fakeplayer.core.constant;
public interface MetadataKeys {
String SELECTION = "fakeplayer:selection";
String REPLENISH = "fakeplayer:replenish";
}

View File

@ -5,6 +5,7 @@ import io.github.hello09x.bedrock.i18n.I18n;
import io.github.hello09x.fakeplayer.api.spi.Action; import io.github.hello09x.fakeplayer.api.spi.Action;
import io.github.hello09x.fakeplayer.core.Main; import io.github.hello09x.fakeplayer.core.Main;
import io.github.hello09x.fakeplayer.core.config.FakeplayerConfig; import io.github.hello09x.fakeplayer.core.config.FakeplayerConfig;
import io.github.hello09x.fakeplayer.core.constant.MetadataKeys;
import io.github.hello09x.fakeplayer.core.entity.FakePlayer; import io.github.hello09x.fakeplayer.core.entity.FakePlayer;
import io.github.hello09x.fakeplayer.core.entity.SpawnOption; import io.github.hello09x.fakeplayer.core.entity.SpawnOption;
import io.github.hello09x.fakeplayer.core.manager.invsee.Invsee; import io.github.hello09x.fakeplayer.core.manager.invsee.Invsee;
@ -356,9 +357,9 @@ public class FakeplayerManager {
*/ */
public void setReplenish(@NotNull Player target, boolean replenish) { public void setReplenish(@NotNull Player target, boolean replenish) {
if (!replenish) { if (!replenish) {
target.removeMetadata("fakeplayer:replenish", Main.getInstance()); target.removeMetadata(MetadataKeys.REPLENISH, Main.getInstance());
} else { } else {
target.setMetadata("fakeplayer:replenish", new FixedMetadataValue(Main.getInstance(), true)); target.setMetadata(MetadataKeys.REPLENISH, new FixedMetadataValue(Main.getInstance(), true));
} }
} }
@ -369,7 +370,7 @@ public class FakeplayerManager {
* @return 是否自动补货 * @return 是否自动补货
*/ */
public boolean isReplenish(@NotNull Player target) { public boolean isReplenish(@NotNull Player target) {
return target.hasMetadata("fakeplayer:replenish"); return target.hasMetadata(MetadataKeys.REPLENISH);
} }
/** /**
@ -380,7 +381,7 @@ public class FakeplayerManager {
*/ */
public void setSelection(@NotNull Player creator, @Nullable Player target) { public void setSelection(@NotNull Player creator, @Nullable Player target) {
if (target == null) { if (target == null) {
creator.removeMetadata("fakeplayer:selection", Main.getInstance()); creator.removeMetadata(MetadataKeys.SELECTION, Main.getInstance());
return; return;
} }
@ -388,7 +389,7 @@ public class FakeplayerManager {
return; return;
} }
creator.setMetadata("fakeplayer:selection", new FixedMetadataValue(Main.getInstance(), target.getUniqueId())); creator.setMetadata(MetadataKeys.SELECTION, new FixedMetadataValue(Main.getInstance(), target.getUniqueId()));
} }
/** /**
@ -401,21 +402,22 @@ public class FakeplayerManager {
if (!(creator instanceof Player p)) { if (!(creator instanceof Player p)) {
return null; return null;
} }
if (!p.hasMetadata("fakeplayer:selection")) { if (!p.hasMetadata(MetadataKeys.SELECTION)) {
return null; return null;
} }
var uuid = (UUID) p.getMetadata("fakeplayer:selection") var uuid = (UUID) p.getMetadata(MetadataKeys.SELECTION)
.stream() .stream()
.map(MetadataValue::value) .map(MetadataValue::value)
.filter(Objects::nonNull) .filter(Objects::nonNull)
.filter(v -> v.getClass() == UUID.class)
.findAny() .findAny()
.orElse(null); .orElse(null);
if (uuid == null) { if (uuid == null) {
return null; return null;
} }
var target = Optional.ofNullable(playerList.getByUUID(uuid)).map(FakePlayer::getPlayer).orElse(null); var target = Optional.ofNullable(this.playerList.getByUUID(uuid)).map(FakePlayer::getPlayer).orElse(null);
if (target == null) { if (target == null) {
this.setSelection(p, null); this.setSelection(p, null);
} }

View File

@ -1,7 +1,7 @@
package io.github.hello09x.fakeplayer.v1_20_R1.spi; package io.github.hello09x.fakeplayer.v1_20_R1.spi;
import io.github.hello09x.fakeplayer.api.spi.NMSServerPlayer; import io.github.hello09x.fakeplayer.api.spi.NMSServerPlayer;
import io.github.hello09x.fakeplayer.core.common.ConstantPool; import io.github.hello09x.fakeplayer.core.constant.ConstantPool;
import io.github.hello09x.fakeplayer.core.util.Reflections; import io.github.hello09x.fakeplayer.core.util.Reflections;
import io.github.hello09x.fakeplayer.v1_20_R1.network.EmptyAdvancements; import io.github.hello09x.fakeplayer.v1_20_R1.network.EmptyAdvancements;
import lombok.Getter; import lombok.Getter;

View File

@ -1,7 +1,7 @@
package io.github.hello09x.fakeplayer.v1_20_R2.spi; package io.github.hello09x.fakeplayer.v1_20_R2.spi;
import io.github.hello09x.fakeplayer.api.spi.NMSServerPlayer; import io.github.hello09x.fakeplayer.api.spi.NMSServerPlayer;
import io.github.hello09x.fakeplayer.core.common.ConstantPool; import io.github.hello09x.fakeplayer.core.constant.ConstantPool;
import io.github.hello09x.fakeplayer.core.util.Reflections; import io.github.hello09x.fakeplayer.core.util.Reflections;
import io.github.hello09x.fakeplayer.v1_20_R2.network.EmptyAdvancements; import io.github.hello09x.fakeplayer.v1_20_R2.network.EmptyAdvancements;
import lombok.Getter; import lombok.Getter;