Commits
Doc authored and md_5 committed 8d52226914c
20 20 | import org.bukkit.block.data.Rotatable; |
21 21 | import org.bukkit.craftbukkit.entity.CraftPlayer; |
22 22 | import org.bukkit.craftbukkit.profile.CraftPlayerProfile; |
23 23 | import org.bukkit.craftbukkit.util.CraftNamespacedKey; |
24 24 | import org.bukkit.profile.PlayerProfile; |
25 25 | import org.jetbrains.annotations.Nullable; |
26 26 | |
27 27 | public class CraftSkull extends CraftBlockEntityState<TileEntitySkull> implements Skull { |
28 28 | |
29 29 | private static final int MAX_OWNER_LENGTH = 16; |
30 - | private GameProfile profile; |
30 + | private ResolvableProfile profile; |
31 31 | |
32 32 | public CraftSkull(World world, TileEntitySkull tileEntity) { |
33 33 | super(world, tileEntity); |
34 34 | } |
35 35 | |
36 36 | protected CraftSkull(CraftSkull state, Location location) { |
37 37 | super(state, location); |
38 38 | } |
39 39 | |
40 40 | |
41 41 | public void load(TileEntitySkull skull) { |
42 42 | super.load(skull); |
43 43 | |
44 44 | ResolvableProfile owner = skull.getOwnerProfile(); |
45 45 | if (owner != null) { |
46 - | profile = owner.gameProfile(); |
46 + | profile = owner; |
47 47 | } |
48 48 | } |
49 49 | |
50 50 | |
51 51 | public boolean hasOwner() { |
52 52 | return profile != null; |
53 53 | } |
54 54 | |
55 55 | |
56 56 | public String getOwner() { |
57 - | return hasOwner() ? profile.getName() : null; |
57 + | return hasOwner() ? profile.name().orElse(null) : null; |
58 58 | } |
59 59 | |
60 60 | |
61 61 | public boolean setOwner(String name) { |
62 62 | if (name == null || name.length() > MAX_OWNER_LENGTH) { |
63 63 | return false; |
64 64 | } |
65 65 | |
66 66 | GameProfile profile = MinecraftServer.getServer().getProfileCache().get(name).orElse(null); |
67 67 | if (profile == null) { |
68 68 | return false; |
69 69 | } |
70 70 | |
71 - | this.profile = profile; |
71 + | this.profile = new ResolvableProfile(profile); |
72 72 | return true; |
73 73 | } |
74 74 | |
75 75 | |
76 76 | public OfflinePlayer getOwningPlayer() { |
77 - | if (profile != null) { |
78 - | if (!profile.getId().equals(SystemUtils.NIL_UUID)) { |
79 - | return Bukkit.getOfflinePlayer(profile.getId()); |
77 + | if (hasOwner()) { |
78 + | if (profile.id().filter(u -> !u.equals(SystemUtils.NIL_UUID)).isPresent()) { |
79 + | return Bukkit.getOfflinePlayer(profile.id().get()); |
80 80 | } |
81 81 | |
82 - | if (!profile.getName().isEmpty()) { |
83 - | return Bukkit.getOfflinePlayer(profile.getName()); |
82 + | if (profile.name().filter(s -> !s.isEmpty()).isPresent()) { |
83 + | return Bukkit.getOfflinePlayer(profile.name().get()); |
84 84 | } |
85 85 | } |
86 86 | |
87 87 | return null; |
88 88 | } |
89 89 | |
90 90 | |
91 91 | public void setOwningPlayer(OfflinePlayer player) { |
92 92 | Preconditions.checkNotNull(player, "player"); |
93 93 | |
94 - | if (player instanceof CraftPlayer) { |
95 - | this.profile = ((CraftPlayer) player).getProfile(); |
94 + | if (player instanceof CraftPlayer craftPlayer) { |
95 + | this.profile = new ResolvableProfile(craftPlayer.getProfile()); |
96 96 | } else { |
97 - | this.profile = new GameProfile(player.getUniqueId(), player.getName()); |
97 + | this.profile = new ResolvableProfile(new GameProfile(player.getUniqueId(), (player.getName() == null) ? "" : player.getName())); |
98 98 | } |
99 99 | } |
100 100 | |
101 101 | |
102 102 | public PlayerProfile getOwnerProfile() { |
103 103 | if (!hasOwner()) { |
104 104 | return null; |
105 105 | } |
106 106 | |
107 107 | return new CraftPlayerProfile(profile); |
108 108 | } |
109 109 | |
110 110 | |
111 111 | public void setOwnerProfile(PlayerProfile profile) { |
112 112 | if (profile == null) { |
113 113 | this.profile = null; |
114 114 | } else { |
115 - | this.profile = CraftPlayerProfile.validateSkullProfile(((CraftPlayerProfile) profile).buildGameProfile()); |
115 + | this.profile = new ResolvableProfile(CraftPlayerProfile.validateSkullProfile(((CraftPlayerProfile) profile).buildGameProfile())); |
116 116 | } |
117 117 | } |
118 118 | |
119 119 | |
120 120 | public NamespacedKey getNoteBlockSound() { |
121 121 | MinecraftKey key = getSnapshot().getNoteBlockSound(); |
122 122 | return (key != null) ? CraftNamespacedKey.fromMinecraft(key) : null; |
123 123 | } |
124 124 | |
125 125 | |
126 126 | public void setNoteBlockSound( NamespacedKey namespacedKey) { |
127 127 | if (namespacedKey == null) { |
128 128 | this.getSnapshot().noteBlockSound = null; |
129 129 | return; |
130 130 | } |
131 131 | this.getSnapshot().noteBlockSound = CraftNamespacedKey.toMinecraft(namespacedKey); |
132 132 | } |
133 133 | |
134 134 | |
135 135 | public BlockFace getRotation() { |
136 136 | BlockData blockData = getBlockData(); |
137 - | return (blockData instanceof Rotatable) ? ((Rotatable) blockData).getRotation() : ((Directional) blockData).getFacing(); |
137 + | return (blockData instanceof Rotatable rotatable) ? rotatable.getRotation() : ((Directional) blockData).getFacing(); |
138 138 | } |
139 139 | |
140 140 | |
141 141 | public void setRotation(BlockFace rotation) { |
142 142 | BlockData blockData = getBlockData(); |
143 143 | if (blockData instanceof Rotatable) { |
144 144 | ((Rotatable) blockData).setRotation(rotation); |
145 145 | } else { |
146 146 | ((Directional) blockData).setFacing(rotation); |
147 147 | } |
180 180 | |
181 181 | public void setSkullType(SkullType skullType) { |
182 182 | throw new UnsupportedOperationException("Must change block type"); |
183 183 | } |
184 184 | |
185 185 | |
186 186 | public void applyTo(TileEntitySkull skull) { |
187 187 | super.applyTo(skull); |
188 188 | |
189 189 | if (getSkullType() == SkullType.PLAYER) { |
190 - | skull.setOwner((profile != null) ? new ResolvableProfile(profile) : null); |
190 + | skull.setOwner(hasOwner() ? profile : null); |
191 191 | } |
192 192 | } |
193 193 | |
194 194 | |
195 195 | public CraftSkull copy() { |
196 196 | return new CraftSkull(this, null); |
197 197 | } |
198 198 | |
199 199 | |
200 200 | public CraftSkull copy(Location location) { |