-
-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preventing NPE caused by creation of PlayerProfile from OfflinePlayer (…
- Loading branch information
Showing
4 changed files
with
81 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,45 @@ | ||
/** | ||
* This file is part of Skript. | ||
* | ||
* Skript is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Skript is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Skript. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* Copyright Peter Güttinger, SkriptLang team and contributors | ||
*/ | ||
package ch.njol.skript.expressions; | ||
|
||
import ch.njol.skript.bukkitutil.ItemUtils; | ||
import org.bukkit.Material; | ||
import org.bukkit.OfflinePlayer; | ||
import org.bukkit.inventory.meta.SkullMeta; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.aliases.Aliases; | ||
import ch.njol.skript.aliases.ItemType; | ||
import ch.njol.skript.doc.Description; | ||
import ch.njol.skript.doc.Examples; | ||
import ch.njol.skript.doc.Name; | ||
import ch.njol.skript.doc.Since; | ||
import ch.njol.skript.expressions.base.SimplePropertyExpression; | ||
import ch.njol.skript.lang.Expression; | ||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
import ch.njol.util.Kleenean; | ||
|
||
/** | ||
* @author Peter Güttinger | ||
*/ | ||
@Name("Player Skull") | ||
@Description("Gets a skull item representing a player. Skulls for other entities are provided by the aliases.") | ||
@Examples({"give the victim's skull to the attacker", | ||
"set the block at the entity to the entity's skull"}) | ||
@Examples({ | ||
"give the victim's skull to the attacker", | ||
"set the block at the entity to the entity's skull" | ||
}) | ||
@Since("2.0") | ||
public class ExprSkull extends SimplePropertyExpression<Object, ItemType> { | ||
public class ExprSkull extends SimplePropertyExpression<OfflinePlayer, ItemType> { | ||
|
||
static { | ||
register(ExprSkull.class, ItemType.class, "(head|skull)", "offlineplayers"); | ||
} | ||
|
||
/** | ||
* In 2017, SkullMeta finally got a method that takes OfflinePlayer. | ||
*/ | ||
private static final boolean newSkullOwner = Skript.methodExists(SkullMeta.class, "setOwningPlayer", OfflinePlayer.class); | ||
|
||
@Override | ||
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) { | ||
return super.init(exprs, matchedPattern, isDelayed, parseResult); | ||
} | ||
|
||
@SuppressWarnings("deprecation") | ||
|
||
@Override | ||
@Nullable | ||
public ItemType convert(final Object o) { | ||
public @Nullable ItemType convert(OfflinePlayer player) { | ||
ItemType skull = new ItemType(Material.PLAYER_HEAD); | ||
SkullMeta meta = (SkullMeta) skull.getItemMeta(); | ||
if (newSkullOwner) | ||
meta.setOwningPlayer((OfflinePlayer) o); | ||
else | ||
meta.setOwner(((OfflinePlayer) o).getName()); | ||
skull.setItemMeta(meta); | ||
ItemUtils.setHeadOwner(skull, player); | ||
return skull; | ||
} | ||
|
||
@Override | ||
public Class<? extends ItemType> getReturnType() { | ||
return ItemType.class; | ||
} | ||
|
||
@Override | ||
protected String getPropertyName() { | ||
return "skull"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters