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

Add glowing sign text support #5367

Merged
merged 15 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
71 changes: 71 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondGlowingText.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* 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.conditions;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import org.bukkit.block.Block;

import ch.njol.skript.conditions.base.PropertyCondition;
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 org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.ItemMeta;

@Name("Has Glowing Text")
@Description("Checks whether a sign (either a block or an item) has glowing text")
@Examples("if target block has glowing text")
@Since("INSERT VERSION")
public class CondGlowingText extends PropertyCondition<Object> {

static {
if (Skript.methodExists(Sign.class, "isGlowingText"))
register(CondGlowingText.class, PropertyType.HAVE, "glowing text", "blocks/itemtypes");
}

@Override
public boolean check(Object obj) {
if (obj instanceof Block) {
BlockState state = ((Block) obj).getState();
return state instanceof Sign && ((Sign) state).isGlowingText();
} else if (obj instanceof ItemType) {
ItemMeta meta = ((ItemType) obj).getItemMeta();
if (meta instanceof BlockStateMeta) {
BlockState state = ((BlockStateMeta) meta).getBlockState();
return state instanceof Sign && ((Sign) state).isGlowingText();
}
}
return false;
}

@Override
protected PropertyType getPropertyType() {
return PropertyType.HAVE;
}

@Override
protected String getPropertyName() {
return "glowing text";
}

}
98 changes: 98 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffGlowingText.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* 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.effects;

import ch.njol.skript.Skript;
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.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;

import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.event.Event;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.eclipse.jdt.annotation.Nullable;

@Name("Make Sign Glow")
@Description("Makes a sign (either a block or item) have glowing text or normal text")
@Examples("make target block of player have glowing text")
@Since("INSERT VERSION")
public class EffGlowingText extends Effect {

static {
if (Skript.methodExists(Sign.class, "setGlowingText", boolean.class)) {
Skript.registerEffect(EffGlowingText.class,
"make %blocks/itemtypes% have glowing text",
"make %blocks/itemtypes% have (normal|non[-| ]glowing) text"
);
}
}

@SuppressWarnings("NotNullFieldNotInitialized")
private Expression<?> objects;

private boolean glowing;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
objects = exprs[0];
glowing = matchedPattern == 0;
return true;
}

@Override
protected void execute(Event event) {
for (Object obj : objects.getArray(event)) {
if (obj instanceof Block) {
BlockState state = ((Block) obj).getState();
if (state instanceof Sign) {
((Sign) state).setGlowingText(glowing);
state.update();
}
} else if (obj instanceof ItemType) {
ItemType item = (ItemType) obj;
ItemMeta meta = item.getItemMeta();
if (!(meta instanceof BlockStateMeta))
return;
BlockStateMeta blockMeta = (BlockStateMeta) meta;
BlockState state = blockMeta.getBlockState();
if (!(state instanceof Sign))
return;
((Sign) state).setGlowingText(glowing);
state.update();
blockMeta.setBlockState(state);
item.setItemMeta(meta);
}
}
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "make " + objects.toString(event, debug) + " have " + (glowing ? "glowing text" : "normal text");
}
APickledWalrus marked this conversation as resolved.
Show resolved Hide resolved

}
18 changes: 18 additions & 0 deletions src/test/skript/tests/syntaxes/effects/EffGlowingText.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
test "glowing sign blocks" when running minecraft "1.17.1":
set {_loc} to spawn of "world"
set {_original block} to type of block at {_loc}
set block at {_loc} to sign
Pikachu920 marked this conversation as resolved.
Show resolved Hide resolved
assert block at {_loc} doesn't have glowing text with "Sign had glowing text erroneously (1)"
make block at {_loc} have glowing text
assert block at {_loc} has glowing text with "Sign had normal text erroneously"
make block at {_loc} have normal text
assert block at {_loc} doesn't have glowing text with "Sign had glowing text erroneously (2)"
set block at {_loc} to {_original block}

test "glowing sign items" when running minecraft "1.17.1":
set {_sign} to sign
assert {_sign} doesn't have glowing text with "Sign had glowing text erroneously (1)"
make {_sign} have glowing text
assert {_sign} has glowing text with "Sign had normal text erroneously"
make {_sign} have normal text
assert {_sign} doesn't have glowing text with "Sign had glowing text erroneously (2)"