-
-
Notifications
You must be signed in to change notification settings - Fork 399
Improve itemframes #7749
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
Open
TheLimeGlass
wants to merge
8
commits into
SkriptLang:dev/feature
Choose a base branch
from
TheLimeGlass:feature/itemframes
base: dev/feature
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Improve itemframes #7749
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
95a364d
Revamp item frames
TheLimeGlass 38466d0
Fix some loading order issues
TheLimeGlass f9a6fe1
usage of java map
TheLimeGlass 482f3b1
Fix java map versioning issues
TheLimeGlass ea039ed
Conflicts
TheLimeGlass 32fc37c
Update to dev/feature
TheLimeGlass 0c6b72d
Nullable annotations
TheLimeGlass 2d3845a
Apply suggestions from code review
TheLimeGlass File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package ch.njol.skript.effects; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import org.bukkit.Rotation; | ||
import org.bukkit.entity.ItemFrame; | ||
import org.bukkit.event.Event; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import ch.njol.skript.Skript; | ||
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.lang.Effect; | ||
import ch.njol.skript.lang.Expression; | ||
import ch.njol.skript.lang.Literal; | ||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
import ch.njol.util.Kleenean; | ||
|
||
@Name("Rotate") | ||
@Description("Rotate rotations or itemframes an amount of times based on the provided rotation.") | ||
@Examples({ | ||
"rotate the event-item frame clockwise 2 times", | ||
"rotate the event-item frame by 225 degrees" | ||
}) | ||
@Since("INSERT VERSION") | ||
public class EffRotate extends Effect { | ||
|
||
static { | ||
Skript.registerEffect(EffRotate.class, "rotate %~rotations% [:counter] clockwise %*number% times"); | ||
Skript.registerEffect(EffRotate.class, "rotate %itemframes% [by] %rotation%"); | ||
} | ||
|
||
private @Nullable Expression<ItemFrame> itemFrames; | ||
private Expression<Rotation> rotations; | ||
private boolean counter; | ||
private int amount; | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
rotations = (Expression<Rotation>) exprs[matchedPattern ^ 0]; | ||
if (matchedPattern == 0) { | ||
amount = Optional.ofNullable(((Literal<Number>) exprs[1])) | ||
.map(Literal::getSingle) | ||
.map(Number::intValue) | ||
.orElse(1); | ||
} else { | ||
itemFrames = (Expression<ItemFrame>) exprs[0]; | ||
} | ||
counter = parseResult.hasTag("counter"); | ||
return true; | ||
} | ||
|
||
@Override | ||
protected void execute(Event event) { | ||
if (itemFrames != null) { | ||
Rotation rotation = rotations.getOptionalSingle(event).orElse(Rotation.NONE); | ||
if (rotation == Rotation.NONE) | ||
return; | ||
itemFrames.stream(event).forEach(itemFrame -> | ||
itemFrame.setRotation(rotate(itemFrame.getRotation(), rotation))); | ||
return; | ||
} | ||
rotations.change(event, rotations.stream(event).map(this::rotate).toArray(Rotation[]::new), ChangeMode.SET); | ||
} | ||
|
||
/** | ||
* The amount of times to rotate clockwise to get to the matched degree. | ||
*/ | ||
private static final Map<Rotation, Integer> order = new HashMap<>(); | ||
|
||
static { | ||
order.put(Rotation.CLOCKWISE_45, 1); | ||
order.put(Rotation.CLOCKWISE, 2); | ||
order.put(Rotation.CLOCKWISE_135, 3); | ||
order.put(Rotation.FLIPPED, 4); | ||
order.put(Rotation.FLIPPED_45, 5); | ||
order.put(Rotation.COUNTER_CLOCKWISE, 6); | ||
order.put(Rotation.COUNTER_CLOCKWISE_45, 7); | ||
} | ||
|
||
private Rotation rotate(Rotation relative, Rotation rotation) { | ||
for (int i = 0; i < order.get(relative); i++) | ||
rotation.rotateClockwise(); | ||
return rotation; | ||
} | ||
|
||
private Rotation rotate(Rotation rotation) { | ||
for (int i = 0; i < amount; i++) { | ||
if (counter) { | ||
rotation.rotateCounterClockwise(); | ||
} else { | ||
rotation.rotateClockwise(); | ||
} | ||
} | ||
return rotation; | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { | ||
String string; | ||
if (itemFrames != null) { | ||
string = "rotate " + itemFrames.toString(event, debug) + " " + rotations.toString(event, debug); | ||
} else { | ||
string = "rotate " + rotations.toString(event, debug) + (counter ? " counter " : "") + " clockwise"; | ||
} | ||
return string + " " + amount + " time" + (amount <= 1 ? "" : "s"); | ||
} | ||
|
||
} |
This file contains hidden or 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 | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,113 @@ | ||||||||||||||||||||
package ch.njol.skript.entity; | ||||||||||||||||||||
|
||||||||||||||||||||
import org.bukkit.Rotation; | ||||||||||||||||||||
import org.bukkit.entity.ItemFrame; | ||||||||||||||||||||
import org.bukkit.inventory.ItemStack; | ||||||||||||||||||||
import org.jetbrains.annotations.Nullable; | ||||||||||||||||||||
|
||||||||||||||||||||
import ch.njol.skript.aliases.ItemType; | ||||||||||||||||||||
import ch.njol.skript.lang.Literal; | ||||||||||||||||||||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||||||||||||||||||||
import ch.njol.skript.localization.Noun; | ||||||||||||||||||||
import ch.njol.skript.registrations.Classes; | ||||||||||||||||||||
|
||||||||||||||||||||
public class ItemFrameData extends EntityData<ItemFrame> { | ||||||||||||||||||||
|
||||||||||||||||||||
static { | ||||||||||||||||||||
EntityData.register(ItemFrameData.class, "item frame", ItemFrame.class, "item frame"); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
private @Nullable ItemType type; | ||||||||||||||||||||
|
||||||||||||||||||||
private Rotation rotation = Rotation.NONE; | ||||||||||||||||||||
|
||||||||||||||||||||
public ItemFrameData() {} | ||||||||||||||||||||
|
||||||||||||||||||||
public ItemFrameData(@Nullable ItemType type, @Nullable Rotation rotation) { | ||||||||||||||||||||
this.rotation = rotation; | ||||||||||||||||||||
this.type = type; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Override | ||||||||||||||||||||
protected boolean init(Literal<?>[] exprs, int matchedPattern, ParseResult parseResult) { | ||||||||||||||||||||
if (exprs[0] != null) | ||||||||||||||||||||
type = (ItemType) exprs[0].getSingle(); | ||||||||||||||||||||
if (exprs[1] != null) | ||||||||||||||||||||
rotation = (Rotation) exprs[1].getSingle(); | ||||||||||||||||||||
return true; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Override | ||||||||||||||||||||
protected boolean init(@Nullable Class<? extends ItemFrame> entityClass, @Nullable ItemFrame itemframe) { | ||||||||||||||||||||
if (itemframe != null) { | ||||||||||||||||||||
ItemStack item = itemframe.getItem(); | ||||||||||||||||||||
type = new ItemType(item); | ||||||||||||||||||||
rotation = itemframe.getRotation(); | ||||||||||||||||||||
} | ||||||||||||||||||||
return true; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Override | ||||||||||||||||||||
protected boolean match(ItemFrame itemframe) { | ||||||||||||||||||||
if (type == null) | ||||||||||||||||||||
return true; | ||||||||||||||||||||
return type.isOfType(itemframe.getItem()) && itemframe.getRotation() == rotation; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Override | ||||||||||||||||||||
public void set(ItemFrame itemframe) { | ||||||||||||||||||||
assert type != null; | ||||||||||||||||||||
ItemStack item = type.getItem().getRandom(); | ||||||||||||||||||||
if (item != null) | ||||||||||||||||||||
itemframe.setItem(item); | ||||||||||||||||||||
itemframe.setRotation(rotation); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Override | ||||||||||||||||||||
public boolean isSupertypeOf(EntityData<?> entityData) { | ||||||||||||||||||||
if (!(entityData instanceof ItemFrameData)) | ||||||||||||||||||||
return false; | ||||||||||||||||||||
ItemFrameData itemFrameData = (ItemFrameData) entityData; | ||||||||||||||||||||
if (type != null) | ||||||||||||||||||||
return itemFrameData.type != null && type.equals(itemFrameData.type) && rotation == itemFrameData.rotation; | ||||||||||||||||||||
Comment on lines
+68
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
return true; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Override | ||||||||||||||||||||
public Class<? extends ItemFrame> getType() { | ||||||||||||||||||||
return ItemFrame.class; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Override | ||||||||||||||||||||
public EntityData<?> getSuperType() { | ||||||||||||||||||||
return new ItemFrameData(type, rotation); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Override | ||||||||||||||||||||
public String toString(int flags) { | ||||||||||||||||||||
if (type == null) | ||||||||||||||||||||
return super.toString(flags); | ||||||||||||||||||||
StringBuilder builder = new StringBuilder(); | ||||||||||||||||||||
builder.append(Noun.getArticleWithSpace(type.getTypes().get(0).getGender(), flags)); | ||||||||||||||||||||
builder.append("item frame " + type == null ? "" : "of " + type.toString(flags)); | ||||||||||||||||||||
builder.append("rotated " + Classes.toString(rotation)); | ||||||||||||||||||||
return builder.toString(); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Override | ||||||||||||||||||||
protected boolean equals_i(EntityData<?> entityData) { | ||||||||||||||||||||
if (!(entityData instanceof ItemFrameData other)) | ||||||||||||||||||||
return false; | ||||||||||||||||||||
return type == null ? true : type.equals(other.type) && rotation == other.rotation; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
@Override | ||||||||||||||||||||
protected int hashCode_i() { | ||||||||||||||||||||
int prime = 31; | ||||||||||||||||||||
int result = 1; | ||||||||||||||||||||
result = prime * result + (type == null ? 0 : type.hashCode()); | ||||||||||||||||||||
result = prime * result + rotation.hashCode(); | ||||||||||||||||||||
return result; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
} |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SSB?