mirror of
https://github.com/LeavesMC/Leaves.git
synced 2025-12-30 04:19:30 +00:00
Fix some fakeplayer feature
This commit is contained in:
@@ -9,6 +9,7 @@ import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.level.storage.LevelResource;
|
||||
import net.minecraft.world.level.storage.LevelStorageSource;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.util.TagUtil;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.io.File;
|
||||
@@ -45,7 +46,7 @@ public class BotDataStorage implements IPlayerDataStorage {
|
||||
public void save(Player player) {
|
||||
boolean flag = true;
|
||||
try {
|
||||
CompoundTag nbt = player.saveWithoutId(new CompoundTag());
|
||||
CompoundTag nbt = TagUtil.saveEntityWithoutId(player);
|
||||
File file = new File(this.botDir, player.getStringUUID() + ".dat");
|
||||
|
||||
if (file.exists() && file.isFile()) {
|
||||
@@ -74,8 +75,8 @@ public class BotDataStorage implements IPlayerDataStorage {
|
||||
|
||||
@Override
|
||||
public Optional<CompoundTag> load(Player player) {
|
||||
return this.load(player.getScoreboardName(), player.getStringUUID()).map((nbt) -> {
|
||||
player.load(nbt);
|
||||
return this.load(player.getScoreboardName(), player.getStringUUID()).map(nbt -> {
|
||||
TagUtil.loadEntity(player, nbt);
|
||||
return nbt;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.storage.TagValueInput;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -28,6 +29,7 @@ import org.leavesmc.leaves.event.bot.BotJoinEvent;
|
||||
import org.leavesmc.leaves.event.bot.BotLoadEvent;
|
||||
import org.leavesmc.leaves.event.bot.BotRemoveEvent;
|
||||
import org.leavesmc.leaves.event.bot.BotSpawnLocationEvent;
|
||||
import org.leavesmc.leaves.util.TagFactory;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.util.List;
|
||||
@@ -129,7 +131,7 @@ public class BotList {
|
||||
location = event.getSpawnLocation();
|
||||
|
||||
bot.spawnIn(world);
|
||||
bot.gameMode.setLevel(bot.serverLevel());
|
||||
bot.gameMode.setLevel(bot.level());
|
||||
|
||||
bot.setPosRaw(location.getX(), location.getY(), location.getZ());
|
||||
bot.setRot(location.getYaw(), location.getPitch());
|
||||
@@ -143,8 +145,9 @@ public class BotList {
|
||||
bot.supressTrackerForLogin = true;
|
||||
world.addNewPlayer(bot);
|
||||
optional.ifPresent(nbt -> {
|
||||
bot.loadAndSpawnEnderPearls(nbt);
|
||||
bot.loadAndSpawnParentVehicle(nbt);
|
||||
TagValueInput input = TagFactory.input(nbt);
|
||||
bot.loadAndSpawnEnderPearls(input);
|
||||
bot.loadAndSpawnParentVehicle(input);
|
||||
});
|
||||
|
||||
BotJoinEvent event1 = new BotJoinEvent(bot.getBukkitEntity(), PaperAdventure.asAdventure(Component.translatable("multiplayer.player.joined", bot.getDisplayName())).style(Style.style(NamedTextColor.YELLOW)));
|
||||
@@ -158,8 +161,8 @@ public class BotList {
|
||||
bot.renderAll();
|
||||
bot.supressTrackerForLogin = false;
|
||||
|
||||
bot.serverLevel().getChunkSource().chunkMap.addEntity(bot);
|
||||
BotList.LOGGER.info("{}[{}] logged in with entity id {} at ([{}]{}, {}, {})", bot.getName().getString(), "Local", bot.getId(), bot.serverLevel().serverLevelData.getLevelName(), bot.getX(), bot.getY(), bot.getZ());
|
||||
bot.level().getChunkSource().chunkMap.addEntity(bot);
|
||||
BotList.LOGGER.info("{}[{}] logged in with entity id {} at ([{}]{}, {}, {})", bot.getName().getString(), "Local", bot.getId(), bot.level().serverLevelData.getLevelName(), bot.getX(), bot.getY(), bot.getZ());
|
||||
return bot;
|
||||
}
|
||||
|
||||
@@ -203,7 +206,7 @@ public class BotList {
|
||||
}
|
||||
|
||||
bot.unRide();
|
||||
bot.serverLevel().removePlayerImmediately(bot, Entity.RemovalReason.UNLOADED_WITH_PLAYER);
|
||||
bot.level().removePlayerImmediately(bot, Entity.RemovalReason.UNLOADED_WITH_PLAYER);
|
||||
this.bots.remove(bot);
|
||||
this.botsByName.remove(bot.getScoreboardName().toLowerCase(Locale.ROOT));
|
||||
|
||||
@@ -214,7 +217,7 @@ public class BotList {
|
||||
}
|
||||
|
||||
bot.removeTab();
|
||||
for (ServerPlayer player : bot.serverLevel().players()) {
|
||||
for (ServerPlayer player : bot.level().players()) {
|
||||
if (!(player instanceof ServerBot)) {
|
||||
player.connection.send(new ClientboundRemoveEntitiesPacket(bot.getId()));
|
||||
}
|
||||
|
||||
@@ -236,7 +236,7 @@ public class ServerBot extends ServerPlayer {
|
||||
this.removeVehicle();
|
||||
}
|
||||
|
||||
ServerLevel fromLevel = this.serverLevel();
|
||||
ServerLevel fromLevel = this.level();
|
||||
ServerLevel toLevel = teleportTransition.newLevel();
|
||||
|
||||
if (toLevel.dimension() == fromLevel.dimension()) {
|
||||
@@ -308,7 +308,7 @@ public class ServerBot extends ServerPlayer {
|
||||
|
||||
@Override
|
||||
public void checkFallDamage(double y, boolean onGround, @NotNull BlockState state, @NotNull BlockPos pos) {
|
||||
ServerLevel serverLevel = this.serverLevel();
|
||||
ServerLevel serverLevel = this.level();
|
||||
if (!this.isInWater() && y < 0.0) {
|
||||
this.fallDistance -= (float) y;
|
||||
}
|
||||
@@ -461,7 +461,7 @@ public class ServerBot extends ServerPlayer {
|
||||
}
|
||||
|
||||
public void sendFakeData(ServerPlayerConnection playerConnection, boolean login) {
|
||||
ChunkMap.TrackedEntity entityTracker = ((ServerLevel) this.level()).getChunkSource().chunkMap.entityMap.get(this.getId());
|
||||
ChunkMap.TrackedEntity entityTracker = this.level().getChunkSource().chunkMap.entityMap.get(this.getId());
|
||||
|
||||
if (entityTracker == null) {
|
||||
LeavesLogger.LOGGER.warning("Fakeplayer cant get entity tracker for " + this.getId());
|
||||
@@ -491,7 +491,7 @@ public class ServerBot extends ServerPlayer {
|
||||
|
||||
@Override
|
||||
public void die(@NotNull DamageSource damageSource) {
|
||||
boolean flag = this.serverLevel().getGameRules().getBoolean(GameRules.RULE_SHOWDEATHMESSAGES);
|
||||
boolean flag = this.level().getGameRules().getBoolean(GameRules.RULE_SHOWDEATHMESSAGES);
|
||||
Component defaultMessage = this.getCombatTracker().getDeathMessage();
|
||||
|
||||
BotDeathEvent event = new BotDeathEvent(this.getBukkitEntity(), PaperAdventure.asAdventure(defaultMessage), flag);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.leavesmc.leaves.bot;
|
||||
|
||||
import io.netty.channel.ChannelFutureListener;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.network.DisconnectionDetails;
|
||||
import net.minecraft.network.PacketSendListener;
|
||||
@@ -19,19 +20,11 @@ public class ServerBotPacketListenerImpl extends ServerGamePacketListenerImpl {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPacket(@NotNull Packet<?> packet) {
|
||||
public void send(@NotNull Packet<?> packet, @Nullable ChannelFutureListener listener) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(@NotNull Packet<?> packet) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(@NotNull Packet<?> packet, @Nullable PacketSendListener callbacks) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect(@NotNull DisconnectionDetails disconnectionInfo, PlayerKickEvent.@NotNull Cause cause) {
|
||||
public void disconnect(@NotNull DisconnectionDetails disconnectionInfo) {
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -71,15 +64,7 @@ public class ServerBotPacketListenerImpl extends ServerGamePacketListenerImpl {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(@NotNull Packet<?> packet) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(@NotNull Packet<?> packet, @Nullable PacketSendListener packetsendlistener) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(@NotNull Packet<?> packet, @Nullable PacketSendListener callbacks, boolean flush) {
|
||||
public void send(@NotNull Packet<?> packet, @javax.annotation.Nullable ChannelFutureListener channelFutureListener, boolean flag) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,13 +23,13 @@ public class UseItemOnAction extends AbstractTimerAction<UseItemOnAction> {
|
||||
public boolean doTick(@NotNull ServerBot bot) {
|
||||
HitResult result = bot.getRayTrace(5, ClipContext.Fluid.NONE);
|
||||
if (result instanceof BlockHitResult blockHitResult) {
|
||||
BlockState state = bot.serverLevel().getBlockState(blockHitResult.getBlockPos());
|
||||
BlockState state = bot.level().getBlockState(blockHitResult.getBlockPos());
|
||||
if (state.isAir()) {
|
||||
return false;
|
||||
}
|
||||
bot.swing(InteractionHand.MAIN_HAND);
|
||||
if (state.getBlock() == Blocks.TRAPPED_CHEST) {
|
||||
BlockEntity entity = bot.serverLevel().getBlockEntity(blockHitResult.getBlockPos());
|
||||
BlockEntity entity = bot.level().getBlockEntity(blockHitResult.getBlockPos());
|
||||
if (entity instanceof TrappedChestBlockEntity chestBlockEntity) {
|
||||
chestBlockEntity.startOpen(bot);
|
||||
Bukkit.getScheduler().runTaskLater(MinecraftInternalPlugin.INSTANCE, () -> chestBlockEntity.stopOpen(bot), 1);
|
||||
|
||||
@@ -23,13 +23,13 @@ public class UseItemOnOffhandAction extends AbstractTimerAction<UseItemOnOffhand
|
||||
public boolean doTick(@NotNull ServerBot bot) {
|
||||
HitResult result = bot.getRayTrace(5, ClipContext.Fluid.NONE);
|
||||
if (result instanceof BlockHitResult blockHitResult) {
|
||||
BlockState state = bot.serverLevel().getBlockState(blockHitResult.getBlockPos());
|
||||
BlockState state = bot.level().getBlockState(blockHitResult.getBlockPos());
|
||||
if (state.isAir()) {
|
||||
return false;
|
||||
}
|
||||
bot.swing(InteractionHand.OFF_HAND);
|
||||
if (state.getBlock() == Blocks.TRAPPED_CHEST) {
|
||||
BlockEntity entity = bot.serverLevel().getBlockEntity(blockHitResult.getBlockPos());
|
||||
BlockEntity entity = bot.level().getBlockEntity(blockHitResult.getBlockPos());
|
||||
if (entity instanceof TrappedChestBlockEntity chestBlockEntity) {
|
||||
chestBlockEntity.startOpen(bot);
|
||||
Bukkit.getScheduler().runTaskLater(MinecraftInternalPlugin.INSTANCE, () -> chestBlockEntity.stopOpen(bot), 1);
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.leavesmc.leaves.util;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.storage.TagValueInput;
|
||||
import net.minecraft.world.level.storage.TagValueOutput;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -25,6 +26,15 @@ public class TagUtil {
|
||||
return entity.save(output);
|
||||
}
|
||||
|
||||
public static CompoundTag saveEntityWithoutId(Entity entity) {
|
||||
if (entity == null) {
|
||||
return new CompoundTag();
|
||||
}
|
||||
TagValueOutput output = TagFactory.output();
|
||||
entity.saveWithoutId(output);
|
||||
return output.buildResult();
|
||||
}
|
||||
|
||||
public static CompoundTag saveTileWithId(@Nullable BlockEntity entity) {
|
||||
if (entity == null) {
|
||||
return new CompoundTag();
|
||||
@@ -42,4 +52,12 @@ public class TagUtil {
|
||||
return entity.saveAsPassenger(output);
|
||||
}
|
||||
|
||||
public static void loadEntity(Entity entity, CompoundTag tag) {
|
||||
if (entity == null) {
|
||||
return;
|
||||
}
|
||||
TagValueInput input = TagFactory.input(tag);
|
||||
entity.load(input);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user