Unicopia/src/main/java/com/minelittlepony/unicopia/Race.java

165 lines
4.4 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia;
2018-09-12 01:29:49 +02:00
2020-04-24 15:23:36 +02:00
import java.util.Arrays;
import java.util.Map;
2020-04-25 13:32:33 +02:00
import java.util.Set;
2020-04-24 15:23:36 +02:00
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
2018-09-12 01:29:49 +02:00
import com.google.common.base.Strings;
import com.minelittlepony.common.client.gui.sprite.TextureSprite;
import com.minelittlepony.common.client.gui.style.Style;
2020-09-23 17:19:28 +02:00
import com.minelittlepony.unicopia.ability.magic.Affine;
import com.minelittlepony.unicopia.item.toxin.FoodType;
2018-09-12 01:29:49 +02:00
2020-04-15 18:12:00 +02:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Identifier;
2020-04-15 18:12:00 +02:00
2020-09-23 17:19:28 +02:00
public enum Race implements Affine {
/**
* The default, unset race.
* This is used if there are no other races.
*/
2020-10-08 09:39:30 +02:00
HUMAN(false, FlightType.NONE, false),
EARTH(false, FlightType.NONE, true),
UNICORN(true, FlightType.NONE, false),
PEGASUS(false, FlightType.AVIAN, false),
BAT(false, FlightType.AVIAN, false),
ALICORN(true, FlightType.AVIAN, true),
CHANGELING(false, FlightType.INSECTOID, false);
2018-09-12 01:29:49 +02:00
private final boolean magic;
2020-10-08 09:39:30 +02:00
private final FlightType flight;
2018-09-12 01:29:49 +02:00
private final boolean earth;
2020-04-24 15:23:36 +02:00
private final static Map<Integer, Race> REGISTRY = Arrays.stream(values()).collect(Collectors.toMap(Enum::ordinal, Function.identity()));
2020-10-08 09:39:30 +02:00
Race(boolean magic, FlightType flight, boolean earth) {
2018-09-12 01:29:49 +02:00
this.magic = magic;
this.flight = flight;
this.earth = earth;
}
2020-09-23 17:19:28 +02:00
@Override
public Affinity getAffinity() {
return this == CHANGELING ? Affinity.BAD : Affinity.NEUTRAL;
}
public boolean canConsume(FoodType type) {
if (!isUsable()) {
return type != FoodType.VEGAN;
}
if (type.isMeat()) {
return this == BAT || this == CHANGELING || this == HUMAN;
}
if (type.isFish()) {
return this == PEGASUS || this == ALICORN;
}
return hasIronGut();
}
2020-04-27 18:09:19 +02:00
public boolean hasIronGut() {
return isUsable() && this != CHANGELING;
}
2020-01-27 11:05:22 +01:00
public boolean isUsable() {
return !isDefault();
}
2018-09-12 01:29:49 +02:00
public boolean isDefault() {
return this == HUMAN;
}
2019-02-09 13:26:03 +01:00
public boolean isOp() {
return this == ALICORN;
}
2020-10-08 09:39:30 +02:00
public FlightType getFlightType() {
2018-09-12 01:29:49 +02:00
return flight;
}
2020-10-08 09:39:30 +02:00
public boolean canFly() {
return !getFlightType().isGrounded();
}
2018-09-12 01:29:49 +02:00
public boolean canCast() {
return magic;
}
public boolean canUseEarth() {
return earth;
}
public boolean canInteractWithClouds() {
2020-04-25 15:46:29 +02:00
return canFly() && this != CHANGELING && this != BAT;
}
2019-01-31 16:21:14 +01:00
public String getTranslationKey() {
return String.format("unicopia.race.%s", name().toLowerCase());
2018-09-12 01:29:49 +02:00
}
public boolean isPermitted(@Nullable PlayerEntity sender) {
2020-04-15 18:12:00 +02:00
if (isOp() && (sender == null || !sender.abilities.creativeMode)) {
return false;
}
Set<Race> whitelist = Unicopia.getConfig().speciesWhiteList.get();
2020-04-25 13:32:33 +02:00
return isDefault()
|| whitelist.isEmpty()
|| whitelist.contains(this);
2020-04-15 18:12:00 +02:00
}
public Race validate(PlayerEntity sender) {
if (!isPermitted(sender)) {
if (this == EARTH) {
return HUMAN;
}
return EARTH.validate(sender);
}
return this;
}
public Style getStyle() {
return new Style()
.setIcon(new TextureSprite()
.setPosition(2, 2)
.setSize(16, 16)
.setTexture(new Identifier("unicopia", "textures/gui/icons.png"))
.setTextureOffset((16 * ordinal()) % 256, (ordinal() / 256) * 16)
)
.setTooltip(getTranslationKey(), 0, 10);
}
2020-04-15 18:12:00 +02:00
2019-02-09 13:26:03 +01:00
public boolean equals(String s) {
2018-09-12 01:29:49 +02:00
return name().equalsIgnoreCase(s)
2019-01-31 16:21:14 +01:00
|| getTranslationKey().equalsIgnoreCase(s);
2018-09-12 01:29:49 +02:00
}
public static Race fromName(String s, Race def) {
2018-09-12 01:29:49 +02:00
if (!Strings.isNullOrEmpty(s)) {
for (Race i : values()) {
2019-02-09 13:26:03 +01:00
if (i.equals(s)) return i;
2018-09-12 01:29:49 +02:00
}
}
try {
return fromId(Integer.parseInt(s));
2018-09-12 01:29:49 +02:00
} catch (NumberFormatException e) { }
return def;
2018-09-12 01:29:49 +02:00
}
public static Race fromName(String name) {
return fromName(name, EARTH);
}
public static Race fromId(int id) {
2020-04-24 15:23:36 +02:00
return REGISTRY.getOrDefault(id, EARTH);
}
2018-09-12 01:29:49 +02:00
}