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

Expression Skull Owner #5698

Merged
Merged
Changes from 1 commit
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
84 changes: 84 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprSkullOwner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* 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.classes.Changer.ChangeMode;
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.util.coll.CollectionUtils;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
import org.bukkit.block.Skull;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

@Name("Skull Owner")
@Description("The skull owner of a player skull.")
@Examples({
"set {_owner} to the skull owner of event-block",
"set skull owner of {_block} to player"
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
})
@Since("INSERT VERSION")
public class ExprSkullOwner extends SimplePropertyExpression<Block, OfflinePlayer> {

static {
register(ExprSkullOwner.class, OfflinePlayer.class, "(head|skull) owner", "blocks");
}

@Override
public @Nullable OfflinePlayer convert(Block block) {
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
if (block.getType().equals(Material.PLAYER_HEAD) || block.getType().equals(Material.PLAYER_WALL_HEAD)) {
return ((Skull) block.getState()).getOwningPlayer();
}
return null;
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public @Nullable Class<?>[] acceptChange(ChangeMode mode) {
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
if (mode == ChangeMode.SET)
return CollectionUtils.array(OfflinePlayer.class);
return null;
}

@Override
public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
for (Block block : getExpr().getArray(event)) {
if (block.getType().equals(Material.PLAYER_HEAD) || block.getType().equals(Material.PLAYER_WALL_HEAD)) {
Skull skull = (Skull) block.getState();
skull.setOwningPlayer((OfflinePlayer) delta[0]);
skull.update();
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
}
}
NotSoDelayed marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public Class<? extends OfflinePlayer> getReturnType() {
return OfflinePlayer.class;
}

@Override
protected String getPropertyName() {
return "skull owner";
}

}