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 tropical fish support #1521

Merged
merged 4 commits into from
Nov 4, 2018
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
150 changes: 150 additions & 0 deletions src/main/java/ch/njol/skript/entity/TropicalFishData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/**
* 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 2011-2017 Peter Güttinger and contributors
*/
package ch.njol.skript.entity;

import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;

import org.bukkit.DyeColor;
import org.bukkit.entity.TropicalFish;
import org.bukkit.entity.TropicalFish.Pattern;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.util.Color;

public class TropicalFishData extends EntityData<TropicalFish> {

@SuppressWarnings("null")
private static Object[] patterns;

static {
if (Skript.isRunningMinecraft(1, 13)) {
register(TropicalFishData.class, "tropical fish", TropicalFish.class, 0,
"tropical fish", "kob", "sunstreak", "snooper",
"dasher", "brinely", "spotty", "flopper",
"stripey", "glitter", "blockfish", "betty", "clayfish");
patterns = Pattern.values();
}
}

public TropicalFishData() {
this(0);
}

public TropicalFishData(Pattern pattern) {
matchedPattern = pattern.ordinal() + 1;
}

private TropicalFishData(int pattern) {
matchedPattern = pattern;
}

@Nullable
private DyeColor patternColor;
@Nullable
private DyeColor bodyColor;

@Override
protected boolean init(Literal<?>[] exprs, int matchedPattern, ParseResult parseResult) {
if (exprs[2] != null) {
bodyColor = ((Literal<Color>) exprs[2]).getSingle().getWoolColor();
patternColor = bodyColor;
}

if (exprs[0] != null)
bodyColor = ((Literal<Color>) exprs[0]).getSingle().getWoolColor();
if (exprs[1] != null)
patternColor = ((Literal<Color>) exprs[1]).getSingle().getWoolColor();

return true;
}

@Override
protected boolean init(@Nullable Class<? extends TropicalFish> c, @Nullable TropicalFish tropicalFish) {
if (tropicalFish != null) {
matchedPattern = tropicalFish.getPattern().ordinal() + 1;
bodyColor = tropicalFish.getBodyColor();
patternColor = tropicalFish.getPatternColor();
}
return true;
}

@Override
public void set(TropicalFish entity) {
if (matchedPattern == 0)
entity.setPattern((Pattern) patterns[ThreadLocalRandom.current().nextInt(patterns.length)]);
else
entity.setPattern((Pattern) patterns[matchedPattern]);

if (bodyColor != null)
entity.setBodyColor(bodyColor);
if (patternColor != null)
entity.setPatternColor(patternColor);
}

@Override
protected boolean match(TropicalFish entity) {
boolean samePattern = matchedPattern == 0 || matchedPattern == entity.getPattern().ordinal() + 1;
boolean sameBody = bodyColor == null || bodyColor == entity.getBodyColor();

if (patternColor == null)
return samePattern && sameBody;
else
return samePattern && sameBody && patternColor == entity.getPatternColor();
}

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

@Override
protected boolean equals_i(EntityData<?> obj) {
if (!(obj instanceof TropicalFishData))
return false;

TropicalFishData other = (TropicalFishData) obj;
return matchedPattern == other.matchedPattern
&& bodyColor == other.bodyColor && patternColor == other.patternColor;
}

@Override
protected int hashCode_i() {
return Objects.hash(matchedPattern, bodyColor, patternColor);
}

@Override
public boolean isSupertypeOf(EntityData<?> e) {
if (!(e instanceof TropicalFishData))
return false;

TropicalFishData other = (TropicalFishData) e;
return matchedPattern == other.matchedPattern
&& bodyColor == other.bodyColor && patternColor == other.patternColor;
}

@Override
public EntityData getSuperType() {
return new TropicalFishData(matchedPattern);
}
}
39 changes: 39 additions & 0 deletions src/main/resources/lang/english.lang
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,45 @@ entities:
fish:
name: fish¦es
pattern: fish(|1¦es)
tropical fish:
name: tropical fish¦es
pattern: [(%-color%[-%-color%]|fully %-color%)] tropical fish(|1¦es)
kob:
name: kob¦s
pattern: [(%-color%[-%-color%]|fully %-color%)] kob(|1¦s)
sunstreak:
name: sunstreak¦s
pattern: [(%-color%[-%-color%]|fully %-color%)] sunstreak(|1¦s)
snooper:
name: snooper¦s
pattern: [(%-color%[-%-color%]|fully %-color%)] snooper(|1¦s)
dasher:
name: dasher¦s
pattern: [(%-color%[-%-color%]|fully %-color%)] dasher(|1¦s)
brinely:
name: brinely¦s
pattern: [(%-color%[-%-color%]|fully %-color%)] brinely(|1¦s)
spotty:
name: spotty¦s
pattern: [(%-color%[-%-color%]|fully %-color%)] spotty(|1¦s)
flopper:
name: flopper¦s
pattern: [(%-color%[-%-color%]|fully %-color%)] flopper(|1¦s)
stripey:
name: stripey¦s
pattern: [(%-color%[-%-color%]|fully %-color%)] stripey(|1¦s)
glitter:
name: glitter¦s
pattern: [(%-color%[-%-color%]|fully %-color%)] glitter(|1¦s)
blockfish:
name: blockfish¦s
pattern: [(%-color%[-%-color%]|fully %-color%)] blockfish(|1¦s)
betty:
name: betty¦s
pattern: [(%-color%[-%-color%]|fully %-color%)] betty(|1¦s)
clayfish:
name: clayfish¦s
pattern: [(%-color%[-%-color%]|fully %-color%)] clayfish(|1¦s)
ghast:
name: ghast¦s
pattern: ghast(|1¦s)
Expand Down