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

Armor Expression #4813

Closed
wants to merge 10 commits into from
73 changes: 73 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprGetAllArmor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* 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 java.util.ArrayList;

import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
public class ExprGetAllArmor extends SimpleExpression<ItemStack> {

static {
Skript.registerExpression(ExprGetAllArmor.class, ItemStack.class, ExpressionType.COMBINED, "%player%'s armor");
}

private Expression<Player> player;

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

@Override
public boolean isSingle() {
return true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you always return multiple values like your array list there. The isSingle should be false.

}

@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parser) {
player = (Expression<Player>) exprs[0];
return true;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "Player: " + player.toString(event, debug);
Copy link
Collaborator

@TheLimeGlass TheLimeGlass Jun 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The toString is to be more descriptive as to where the syntax is coming from. Something like this below;
Also event can be null if debug.

if (event == null || debug)
    return "armour of player";
return "armour collection of " + players.toString(event, debug);

}

@Override
protected @Nullable ItemStack[] get(Event event) {
Player p = player.getSingle(event);
if (p != null) {
Copy link
Collaborator

@TheLimeGlass TheLimeGlass Jun 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be made into a singleton condition rather than having arrow code. You can also use this. to keep the variable name.

Player player = this.player.getSingle(event);
if (player == null)
    return null;
// Continue code

org.bukkit.inventory.PlayerInventory inv = p.getInventory();
ItemStack[] armor={inv.getHelmet(),inv.getChestplate(),inv.getLeggings(),inv.getBoots()};
return armor;
}
return null;
}
}