Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Multiversion] Support for 1.21.40 #333

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ public class Player extends EntityHuman implements CommandSender, InventoryHolde
protected CraftingTransaction craftingTransaction;
protected EnchantTransaction enchantTransaction;
protected RepairItemTransaction repairItemTransaction;
protected LoomTransaction loomTransaction;
protected SmithingTransaction smithingTransaction;
protected TradingTransaction tradingTransaction;

Expand Down Expand Up @@ -3352,6 +3353,18 @@ public void onCompletion(Server server) {
}

if (protocol >= ProtocolInfo.v1_20_30_24) {
if (this.protocol >= ProtocolInfo.v1_21_40) {
if (authPacket.getInputData().contains(AuthInputAction.STOP_SPIN_ATTACK)) {
PlayerToggleSpinAttackEvent playerToggleSpinAttackEvent = new PlayerToggleSpinAttackEvent(this, false);
this.server.getPluginManager().callEvent(playerToggleSpinAttackEvent);
if (playerToggleSpinAttackEvent.isCancelled()) {
this.needSendData = true;
} else {
this.setSpinAttack(false);
}
}
}

if (authPacket.getInputData().contains(AuthInputAction.START_FLYING)) {
if (!server.getAllowFlight() && !this.getAdventureSettings().get(Type.ALLOW_FLIGHT)) {
this.kick(PlayerKickEvent.Reason.FLYING_DISABLED, "Flying is not enabled on this server");
Expand Down Expand Up @@ -3615,6 +3628,7 @@ public void onCompletion(Server server) {
}
break;
case PlayerActionPacket.ACTION_STOP_SPIN_ATTACK:
if (this.isMovementServerAuthoritative() && protocol >= ProtocolInfo.v1_21_40) break;
PlayerToggleSpinAttackEvent playerToggleSpinAttackEvent = new PlayerToggleSpinAttackEvent(this, false);
playerToggleSpinAttackEvent.call();
if (playerToggleSpinAttackEvent.isCancelled()) {
Expand Down Expand Up @@ -4099,6 +4113,23 @@ public void onCompletion(Server server) {
}

if (transactionPacket.isCraftingPart) {
if (LoomTransaction.checkForItemPart(actions)) {
if (this.loomTransaction == null) {
this.loomTransaction = new LoomTransaction(this, actions);
} else {
for (InventoryAction action : actions) {
this.loomTransaction.addAction(action);
}
}
if (this.loomTransaction.canExecute()) {
if (this.loomTransaction.execute()) {
level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_BLOCK_LOOM_USE);
}
}
this.loomTransaction = null;
return;
}

if (this.craftingTransaction == null) {
this.craftingTransaction = new CraftingTransaction(this, actions);
} else {
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/cn/nukkit/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,10 @@ public Level remove(@NotNull Object key) {
* A number of datagram packets each address can send within one RakNet tick (10ms)
*/
public int rakPacketLimit;
/**
* Temporary disable world saving to allow safe backup of leveldb worlds.
*/
public boolean holdWorldSave;

Server(final String filePath, String dataPath, String pluginPath, boolean loadPlugins, boolean debug) {
Preconditions.checkState(instance == null, "Already initialized!");
Expand Down Expand Up @@ -1067,6 +1071,10 @@ public void forceShutdown(String reason) {
ServerStopEvent serverStopEvent = new ServerStopEvent();
pluginManager.callEvent(serverStopEvent);

if (this.holdWorldSave) {
this.getLogger().warning("World save hold was not released! Any backup currently being taken may be invalid");
}

if (this.rcon != null) {
this.getLogger().debug("Closing RCON...");
this.rcon.close();
Expand Down Expand Up @@ -1301,7 +1309,9 @@ public void sendFullPlayerListData(Player player) {
}

public void sendRecipeList(Player player) {
if (player.protocol >= ProtocolInfo.v1_21_30) {
if (player.protocol >= ProtocolInfo.v1_21_40) {
player.dataPacket(CraftingManager.packet748);
} else if (player.protocol >= ProtocolInfo.v1_21_30) {
player.dataPacket(CraftingManager.packet729);
} else if (player.protocol >= ProtocolInfo.v1_21_20) {
player.dataPacket(CraftingManager.packet712);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/cn/nukkit/block/BlockCoralBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public int onUpdate(int type) {
}
}
BlockFadeEvent event = new BlockFadeEvent(this, new BlockCoralBlock(this.getDamage() | 0x8));
event.call();
if (!event.isCancelled()) {
this.setDead(true);
this.getLevel().setBlock(this, event.getNewState(), true, true);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/cn/nukkit/block/BlockCoralFan.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public int onUpdate(int type) {

if (!this.isDead() && !(this.getLevelBlockAtLayer(1) instanceof BlockWater) && !(this.getLevelBlockAtLayer(1) instanceof BlockIceFrosted)) {
BlockFadeEvent event = new BlockFadeEvent(this, new BlockCoralFanDead(this.getDamage()));
event.call();
if (!event.isCancelled()) {
this.getLevel().setBlock(this, event.getNewState(), true, true);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/cn/nukkit/block/BlockSeaPickle.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public int onUpdate(int type) {
}
} else if (!this.isDead()) {
BlockFadeEvent event = new BlockFadeEvent(this, new BlockSeaPickle(this.getDamage() ^ 0x4));
event.call();
if (!event.isCancelled()) {
this.getLevel().setBlock(this, event.getNewState(), true, true);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cn/nukkit/block/BlockVinesNether.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ && findVineAge(true).orElse(maxVineAge) < maxVineAge) {
*/
public boolean grow() {
Block pos = getSide(getGrowthDirection());
if (pos.getId() != AIR || pos.y < 0 || 255 < pos.y) {
if (pos.getId() != AIR || pos.y < this.level.getMinBlockY() || pos.y > this.level.getMaxBlockY()) {
return false;
}

Expand Down Expand Up @@ -162,7 +162,7 @@ public int growMultiple() {
int grew = 0;
for (int distance = 1; distance <= blocksToGrow; distance++) {
Block pos = getSide(growthDirection, distance);
if (pos.getId() != AIR || pos.y < 0 || 255 < pos.y) {
if (pos.getId() != AIR || pos.y < this.level.getMinBlockY() || pos.y > this.level.getMaxBlockY()) {
break;
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/cn/nukkit/blockentity/BlockEntityBell.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public void saveNBT() {

@Override
public boolean onUpdate() {
if (this.closed) {
return false;
}

if (ringing) {
if (ticks == 0) {
level.addSound(this, Sound.BLOCK_BELL_HIT);
Expand All @@ -81,10 +85,6 @@ public boolean onUpdate() {
}

private void spawnToAllWithExceptions() {
if (this.closed) {
return;
}

for (Player player : this.getLevel().getChunkPlayers(this.chunk.getX(), this.chunk.getZ()).values()) {
if (player.spawned && !spawnExceptions.contains(player)) {
this.spawnTo(player);
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/cn/nukkit/command/SimpleCommandMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private void setDefaultCommands() {
this.register("nukkit", new KickCommand("kick"));
this.register("nukkit", new OpCommand("op"));
this.register("nukkit", new DeopCommand("deop"));
this.register("nukkit", new SaveCommand("save-all"));
this.register("nukkit", new SaveCommand("save"));
this.register("nukkit", new GiveCommand("give"));
this.register("nukkit", new EffectCommand("effect"));
this.register("nukkit", new EnchantCommand("enchant"));
Expand All @@ -70,8 +70,6 @@ private void setDefaultCommands() {
this.register("nukkit", new DefaultGamemodeCommand("defaultgamemode"));
this.register("nukkit", new SayCommand("say"));
this.register("nukkit", new MeCommand("me"));
this.register("nukkit", new SaveOnCommand("save-on"));
this.register("nukkit", new SaveOffCommand("save-off"));
this.register("nukkit", new DifficultyCommand("difficulty"));
this.register("nukkit", new ParticleCommand("particle"));
this.register("nukkit", new SpawnpointCommand("spawnpoint"));
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/cn/nukkit/command/defaults/SaveCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)
return true;
}

if (args.length > 0) {
switch (args[0].toLowerCase()) {
case "on":
sender.getServer().setAutoSave(true);
Command.broadcastCommandMessage(sender, new TranslationContainer("commands.save.enabled"));
return true;
case "off":
sender.getServer().setAutoSave(false);
Command.broadcastCommandMessage(sender, new TranslationContainer("commands.save.disabled"));
return true;
case "hold":
sender.getServer().holdWorldSave = true;
Command.broadcastCommandMessage(sender, new TranslationContainer("commands.save.hold-on"));
return true;
case "resume":
sender.getServer().holdWorldSave = false;
Command.broadcastCommandMessage(sender, new TranslationContainer("commands.save.hold-off"));
return true;
default:
sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
return false;
}
}

broadcastCommandMessage(sender, new TranslationContainer("commands.save.start"));

for (Player player : sender.getServer().getOnlinePlayers().values()) {
Expand Down
28 changes: 0 additions & 28 deletions src/main/java/cn/nukkit/command/defaults/SaveOffCommand.java

This file was deleted.

28 changes: 0 additions & 28 deletions src/main/java/cn/nukkit/command/defaults/SaveOnCommand.java

This file was deleted.

23 changes: 14 additions & 9 deletions src/main/java/cn/nukkit/entity/data/EntityMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@
import cn.nukkit.math.Vector3;
import cn.nukkit.math.Vector3f;
import cn.nukkit.nbt.tag.CompoundTag;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
* @author MagicDroidX
* Nukkit Project
*/
public class EntityMetadata {

private Map<Integer, EntityData> map = new HashMap<>();
private Int2ObjectMap<EntityData> map;

public EntityMetadata() {
this.map = new Int2ObjectOpenHashMap<>();
}

private EntityMetadata(Int2ObjectMap<EntityData> map) {
this.map = map;
}

public EntityData get(int id) {
return this.getOrDefault(id, null);
Expand Down Expand Up @@ -117,16 +127,11 @@ public EntityMetadata putString(int id, String value) {
}

public Map<Integer, EntityData> getMap() {
return new HashMap<>(map);
}

private EntityMetadata replace(Map<Integer, EntityData> map) {
this.map = map;
return this;
return new TreeMap<>(this.map); // Ordered
}

@Override
public EntityMetadata clone() {
return new EntityMetadata().replace(this.getMap());
return new EntityMetadata(new Int2ObjectOpenHashMap<>(this.map));
}
}
33 changes: 33 additions & 0 deletions src/main/java/cn/nukkit/event/inventory/LoomItemEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cn.nukkit.event.inventory;

import cn.nukkit.Player;
import cn.nukkit.event.Cancellable;
import cn.nukkit.event.HandlerList;
import cn.nukkit.inventory.LoomInventory;
import cn.nukkit.item.Item;

public class LoomItemEvent extends InventoryEvent implements Cancellable {

private static final HandlerList handlers = new HandlerList();

public static HandlerList getHandlers() {
return handlers;
}

private final Item newItem;
private final Player player;

public LoomItemEvent(LoomInventory inventory, Item newItem, Player player) {
super(inventory);
this.newItem = newItem;
this.player = player;
}

public Item getNewItem() {
return this.newItem;
}

public Player getPlayer() {
return this.player;
}
}
2 changes: 2 additions & 0 deletions src/main/java/cn/nukkit/inventory/CraftingManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class CraftingManager {
public static DataPacket packet685;
public static DataPacket packet712;
public static DataPacket packet729;
public static DataPacket packet748;

private final Map<Integer, Map<UUID, ShapedRecipe>> shapedRecipes313 = new Int2ObjectOpenHashMap<>();
private final Map<Integer, Map<UUID, ShapedRecipe>> shapedRecipes332 = new Int2ObjectOpenHashMap<>();
Expand Down Expand Up @@ -734,6 +735,7 @@ private CraftingDataPacket packetFor(int protocol) {

public void rebuildPacket() {
//TODO Multiversion 添加新版本支持时修改这里
packet748 = packetFor(ProtocolInfo.v1_21_40).compress(Deflater.BEST_COMPRESSION);
packet729 = packetFor(ProtocolInfo.v1_21_30).compress(Deflater.BEST_COMPRESSION);
packet712 = packetFor(ProtocolInfo.v1_21_20).compress(Deflater.BEST_COMPRESSION);
packet685 = packetFor(ProtocolInfo.v1_21_0).compress(Deflater.BEST_COMPRESSION);
Expand Down
Loading