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

stuff #5934

Merged
merged 3 commits into from
Sep 14, 2024
Merged

stuff #5934

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
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ public boolean onCommandEssentials(final CommandSender cSender, final Command co
} catch (final Exception ex) {
LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
if (cSender instanceof Player) {
cSender.sendMessage(tlLocale(I18n.getLocale(getPlayerLocaleProvider().getLocale((Player) cSender)), "internalError"));
getBukkitAudience().sender(cSender).sendMessage(AdventureUtil.miniMessage().deserialize(tlLocale(I18n.getLocale(getPlayerLocaleProvider().getLocale((Player) cSender)), "internalError")));
} else {
cSender.sendMessage(tlLiteral("internalError"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void setWarp(final IUser user, final String name, final Location loc) thr
if (conf == null) {
final File confFile = new File(warpsFolder, filename + ".yml");
if (confFile.exists()) {
throw new Exception(user == null ? tlLiteral("similarWarpExist") : user.playerTl("similarWarpExist"));
throw new TranslatableException("similarWarpExist");
}
conf = new EssentialsConfiguration(confFile);
conf.load();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.earth2me.essentials.Mob;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.RegistryUtil;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.entity.Cat;
Expand All @@ -21,8 +22,12 @@ public Commandkittycannon() {

private static Ocelot spawnOcelot(final Server server, final User user) throws Mob.MobException {
final Ocelot ocelot = (Ocelot) Mob.OCELOT.spawn(user.getWorld(), server, user.getBase().getEyeLocation());
final int i = random.nextInt(Ocelot.Type.values().length);
ocelot.setCatType(Ocelot.Type.values()[i]);
//noinspection deprecation
final Object[] values = RegistryUtil.values(Ocelot.Type.class);

final int i = random.nextInt(values.length);
//noinspection deprecation
ocelot.setCatType((Ocelot.Type) values[i]);
((Tameable) ocelot).setTamed(true);
ocelot.setBaby();
ocelot.setVelocity(user.getBase().getEyeLocation().getDirection().multiply(2));
Expand All @@ -31,8 +36,10 @@ private static Ocelot spawnOcelot(final Server server, final User user) throws M

private static Entity spawnCat(final Server server, final User user) throws Mob.MobException {
final Cat cat = (Cat) Mob.CAT.spawn(user.getWorld(), server, user.getBase().getEyeLocation());
final int i = random.nextInt(Cat.Type.values().length);
cat.setCatType(Cat.Type.values()[i]);
final Object[] values = RegistryUtil.values(Cat.Type.class);

final int i = random.nextInt(values.length);
cat.setCatType((Cat.Type) values[i]);
cat.setTamed(true);
cat.setBaby();
cat.setVelocity(user.getBase().getEyeLocation().getDirection().multiply(2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,50 @@
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public final class RegistryUtil {
private static final Table<Class<?>, String, Object> registryCache = HashBasedTable.create();

private RegistryUtil() {
}

public static <T> Object[] values(Class<T> registry) {
if (registry.getEnumConstants() != null) {
return registry.getEnumConstants();
}

//noinspection unchecked
final T[] values = (T[]) registryCache.get(registry, "$values");
if (values != null) {
return values;
}

final List<T> set = new ArrayList<>();

for (final Field field : registry.getDeclaredFields()) {
try {
final Object value = field.get(null);
if (value != null && registry.isAssignableFrom(value.getClass())) {
//noinspection unchecked
set.add((T) value);
}
} catch (NullPointerException | IllegalAccessException ignored) {
}
}

//noinspection unchecked
final T[] array = (T[]) new Object[set.size()];
for (int i = 0; i < set.size(); i++) {
array[i] = set.get(i);
}
registryCache.put(registry, "$values", array);

return array;
}

public static <T> T valueOf(Class<T> registry, String... names) {
for (final String name : names) {
//noinspection unchecked
Expand Down
2 changes: 1 addition & 1 deletion Essentials/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ discordCommandAccountResponseLinkedOther={0}'s account is linked to the Minecraf
discordCommandAccountResponseNotLinked=You do not have a linked Minecraft account.
discordCommandAccountResponseNotLinkedOther={0} does not have a linked Minecraft account.
discordCommandDescription=Sends the discord invite link to the player.
discordCommandLink=<primary>Join our Discord server at <secondary>{0}<primary>\!
discordCommandLink=<primary>Join our Discord server at <secondary><click:open_url:"{0}">{0}</click><primary>\!
discordCommandUsage=/<command>
discordCommandUsage1=/<command>
discordCommandUsage1Description=Sends the discord invite link to the player
Expand Down
Loading