Skip to content

Commit ccfa977

Browse files
authored
Merge branch 'SlimefunGuguProject:dev' into dev
2 parents 9501c00 + 1849352 commit ccfa977

10 files changed

Lines changed: 102 additions & 44 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package city.norain.slimefun4.compatibillty;
2+
3+
import city.norain.slimefun4.SlimefunExtended;
4+
import lombok.experimental.UtilityClass;
5+
import org.bukkit.Material;
6+
import org.bukkit.NamespacedKey;
7+
import org.bukkit.OfflinePlayer;
8+
import org.bukkit.Registry;
9+
import org.bukkit.attribute.Attribute;
10+
import org.bukkit.block.data.Ageable;
11+
import org.bukkit.block.data.BlockData;
12+
import org.bukkit.block.data.type.WallSign;
13+
14+
@UtilityClass
15+
public class CompatibilityUtil {
16+
/**
17+
* 获取玩家放置此方块所使用的物品材质。
18+
* 对于大多数方块,这与 getMaterial() 相同,但有些方块有不同的材质用于放置它们。
19+
* 注意:此处没有涵盖所有可能不同的方块数据类型。
20+
*
21+
* @param blockData
22+
* @return 放置此方块所使用的材质
23+
*/
24+
public Material getPlacementMaterial(BlockData blockData) {
25+
if (SlimefunExtended.getMinecraftVersion().isAtLeast(1, 19, 4)) {
26+
return blockData.getPlacementMaterial();
27+
} else {
28+
switch (blockData.getMaterial()) {
29+
case PLAYER_WALL_HEAD -> {
30+
return Material.PLAYER_HEAD;
31+
}
32+
case REDSTONE_WIRE -> {
33+
return Material.REDSTONE;
34+
}
35+
default -> {
36+
var mat = blockData.getMaterial();
37+
var enumName = blockData.getMaterial().name();
38+
39+
if (Ageable.class.equals(mat.data) && enumName.endsWith("S")) {
40+
var itemMat = Material.getMaterial(enumName.substring(0, enumName.length() - 1));
41+
return itemMat != null && itemMat.isItem() ? itemMat : mat;
42+
}
43+
44+
if (WallSign.class.equals(mat.data) && enumName.contains("_WALL_")) {
45+
Material itemMat = Material.getMaterial(enumName.replace("_WALL_", "_"));
46+
47+
if (itemMat != null && itemMat.isItem()) {
48+
return mat;
49+
}
50+
}
51+
52+
// Fallback to original material
53+
return blockData.getMaterial();
54+
}
55+
}
56+
}
57+
}
58+
59+
/**
60+
* 检查玩家是否处于连接状态。
61+
* 在 1.20- 中不能保证玩家是否连接,仅返回在线状态。
62+
*
63+
* @param player 离线玩家
64+
* @return 玩家连接或在线
65+
*/
66+
public boolean isConnected(OfflinePlayer player) {
67+
if (SlimefunExtended.getMinecraftVersion().isAtLeast(1, 20)) {
68+
return player.isConnected();
69+
} else {
70+
return player.isOnline();
71+
}
72+
}
73+
74+
/**
75+
* 获取最大生命值属性。
76+
* 在 1.21.3 之前,使用 GENERIC_MAX_HEALTH。
77+
*
78+
* @return 最大生命值属性
79+
*/
80+
public static Attribute getMaxHealth() {
81+
if (SlimefunExtended.getMinecraftVersion().isAtLeast(1, 21, 3)) {
82+
return Registry.ATTRIBUTE.get(NamespacedKey.fromString("max_health"));
83+
} else {
84+
return Attribute.valueOf("GENERIC_MAX_HEALTH");
85+
}
86+
}
87+
}

src/main/java/city/norain/slimefun4/compatibillty/VersionedAttribute.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/main/java/city/norain/slimefun4/utils/InventoryUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package city.norain.slimefun4.utils;
22

3+
import city.norain.slimefun4.compatibillty.CompatibilityUtil;
34
import io.github.thebusybiscuit.slimefun4.core.debug.Debug;
45
import io.github.thebusybiscuit.slimefun4.core.debug.TestCase;
56
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
@@ -19,7 +20,7 @@ public void openInventory(Player p, Inventory inventory) {
1920
return;
2021
}
2122

22-
if (!PlayerUtil.isConnected(p)) {
23+
if (!CompatibilityUtil.isConnected(p)) {
2324
Debug.log(
2425
TestCase.INVENTORY,
2526
"Tried to open an inventory for a player that is not connected: " + p.getName());

src/main/java/city/norain/slimefun4/utils/PlayerUtil.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/main/java/com/xzavier0722/mc/plugin/slimefun4/storage/controller/ProfileDataController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.xzavier0722.mc.plugin.slimefun4.storage.controller;
22

3-
import city.norain.slimefun4.utils.PlayerUtil;
3+
import city.norain.slimefun4.compatibillty.CompatibilityUtil;
44
import com.xzavier0722.mc.plugin.slimefun4.storage.callback.IAsyncReadCallback;
55
import com.xzavier0722.mc.plugin.slimefun4.storage.common.DataScope;
66
import com.xzavier0722.mc.plugin.slimefun4.storage.common.DataType;
@@ -356,7 +356,7 @@ public void run() {
356356
return;
357357
}
358358

359-
if (PlayerUtil.isConnected(Bukkit.getOfflinePlayer(UUID.fromString(pUuid)))) {
359+
if (CompatibilityUtil.isConnected(Bukkit.getOfflinePlayer(UUID.fromString(pUuid)))) {
360360
return;
361361
}
362362

src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/medical/Bandage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.github.thebusybiscuit.slimefun4.implementation.items.medical;
22

3-
import city.norain.slimefun4.compatibillty.VersionedAttribute;
3+
import city.norain.slimefun4.compatibillty.CompatibilityUtil;
44
import io.github.bakedlibs.dough.items.ItemUtils;
55
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
66
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
@@ -48,7 +48,7 @@ public ItemUseHandler getItemHandler() {
4848
// Player is neither burning nor injured
4949
if (p.getFireTicks() <= 0
5050
&& p.getHealth()
51-
>= p.getAttribute(VersionedAttribute.getMaxHealth()).getValue()) {
51+
>= p.getAttribute(CompatibilityUtil.getMaxHealth()).getValue()) {
5252
return;
5353
}
5454

src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/medical/MedicalSupply.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.github.thebusybiscuit.slimefun4.implementation.items.medical;
22

3-
import city.norain.slimefun4.compatibillty.VersionedAttribute;
3+
import city.norain.slimefun4.compatibillty.CompatibilityUtil;
44
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
55
import io.github.thebusybiscuit.slimefun4.api.items.ItemHandler;
66
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
@@ -71,7 +71,7 @@ public void clearNegativeEffects(@Nonnull LivingEntity n) {
7171
*/
7272
public void heal(@Nonnull LivingEntity n) {
7373
double health = n.getHealth() + healAmount;
74-
double maxHealth = n.getAttribute(VersionedAttribute.getMaxHealth()).getValue();
74+
double maxHealth = n.getAttribute(CompatibilityUtil.getMaxHealth()).getValue();
7575
n.setHealth(Math.min(health, maxHealth));
7676
}
7777
}

src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/medical/Splint.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.github.thebusybiscuit.slimefun4.implementation.items.medical;
22

3-
import city.norain.slimefun4.compatibillty.VersionedAttribute;
3+
import city.norain.slimefun4.compatibillty.CompatibilityUtil;
44
import io.github.bakedlibs.dough.items.ItemUtils;
55
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
66
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
@@ -36,7 +36,7 @@ public Splint(
3636
// Player is neither burning nor injured
3737
if (p.getFireTicks() <= 0
3838
&& p.getHealth()
39-
>= p.getAttribute(VersionedAttribute.getMaxHealth()).getValue()) {
39+
>= p.getAttribute(CompatibilityUtil.getMaxHealth()).getValue()) {
4040
return;
4141
}
4242

src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/weapons/VampireBlade.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.github.thebusybiscuit.slimefun4.implementation.items.weapons;
22

3-
import city.norain.slimefun4.compatibillty.VersionedAttribute;
3+
import city.norain.slimefun4.compatibillty.CompatibilityUtil;
44
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
55
import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting;
66
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
@@ -43,7 +43,7 @@ public VampireBlade(ItemGroup itemGroup, SlimefunItemStack item, RecipeType reci
4343
SoundEffect.VAMPIRE_BLADE_HEALING_SOUND.playFor(p);
4444
double health = p.getHealth() + HEALING_AMOUNT;
4545
double maxHealth =
46-
p.getAttribute(VersionedAttribute.getMaxHealth()).getValue();
46+
p.getAttribute(CompatibilityUtil.getMaxHealth()).getValue();
4747
p.setHealth(Math.min(health, maxHealth));
4848
}
4949
};

src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BlockListener.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.thebusybiscuit.slimefun4.implementation.listeners;
22

3+
import city.norain.slimefun4.compatibillty.CompatibilityUtil;
34
import com.xzavier0722.mc.plugin.slimefun4.storage.callback.IAsyncReadCallback;
45
import com.xzavier0722.mc.plugin.slimefun4.storage.controller.SlimefunBlockData;
56
import com.xzavier0722.mc.plugin.slimefun4.storage.controller.attributes.UniversalBlock;
@@ -116,7 +117,8 @@ public void onBlockPlace(BlockPlaceEvent e) {
116117
if (sfItem != null && !(sfItem instanceof NotPlaceable)) {
117118
// Fixes #994, should check placed block is equals to item material or not.
118119
if (item.getType() != e.getBlock().getType()) {
119-
if (item.getType() != e.getBlock().getBlockData().getPlacementMaterial()) {
120+
if (item.getType()
121+
!= CompatibilityUtil.getPlacementMaterial(e.getBlock().getBlockData())) {
120122
return;
121123
}
122124
}

0 commit comments

Comments
 (0)