1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 11:45:51 +00:00

Impelemented a few instanceof patterns, actually quite nice :)

This commit is contained in:
TheBusyBiscuit 2022-07-02 21:43:16 +02:00
parent 20c3564e8f
commit bb9e2dfa4a
6 changed files with 33 additions and 39 deletions

View File

@ -327,8 +327,8 @@ public class ItemGroup implements Keyed {
@Override @Override
public final boolean equals(Object obj) { public final boolean equals(Object obj) {
if (obj instanceof ItemGroup) { if (obj instanceof ItemGroup group) {
return ((ItemGroup) obj).getKey().equals(getKey()); return group.getKey().equals(this.getKey());
} else { } else {
return false; return false;
} }

View File

@ -482,8 +482,8 @@ public class SlimefunItem implements Placeable {
} }
// Lock the SlimefunItemStack from any accidental manipulations // Lock the SlimefunItemStack from any accidental manipulations
if (itemStackTemplate instanceof SlimefunItemStack && isItemStackImmutable()) { if (itemStackTemplate instanceof SlimefunItemStack stack && isItemStackImmutable()) {
((SlimefunItemStack) itemStackTemplate).lock(); stack.lock();
} }
postRegister(); postRegister();
@ -755,8 +755,8 @@ public class SlimefunItem implements Placeable {
} }
// If the given item is a SlimefunitemStack, simply compare the id // If the given item is a SlimefunitemStack, simply compare the id
if (item instanceof SlimefunItemStack) { if (item instanceof SlimefunItemStack stack) {
return getId().equals(((SlimefunItemStack) item).getItemId()); return getId().equals(stack.getItemId());
} }
if (item.hasItemMeta()) { if (item.hasItemMeta()) {
@ -807,10 +807,10 @@ public class SlimefunItem implements Placeable {
itemhandlers.put(handler.getIdentifier(), handler); itemhandlers.put(handler.getIdentifier(), handler);
// Tickers are a special case (at the moment at least) // Tickers are a special case (at the moment at least)
if (handler instanceof BlockTicker) { if (handler instanceof BlockTicker ticker) {
ticking = true; ticking = true;
Slimefun.getRegistry().getTickerBlocks().add(getId()); Slimefun.getRegistry().getTickerBlocks().add(getId());
blockTicker = (BlockTicker) handler; blockTicker = ticker;
} }
} }
} }
@ -1033,8 +1033,8 @@ public class SlimefunItem implements Placeable {
addon.getLogger().log(Level.SEVERE, message, throwable); addon.getLogger().log(Level.SEVERE, message, throwable);
// We definitely want to re-throw them during Unit Tests // We definitely want to re-throw them during Unit Tests
if (throwable instanceof RuntimeException && Slimefun.getMinecraftVersion() == MinecraftVersion.UNIT_TEST) { if (throwable instanceof RuntimeException e && Slimefun.getMinecraftVersion() == MinecraftVersion.UNIT_TEST) {
throw (RuntimeException) throwable; throw e;
} }
} }
@ -1122,8 +1122,8 @@ public class SlimefunItem implements Placeable {
@Override @Override
public final boolean equals(Object obj) { public final boolean equals(Object obj) {
if (obj instanceof SlimefunItem) { if (obj instanceof SlimefunItem item) {
return ((SlimefunItem) obj).getId().equals(getId()); return item.getId().equals(this.getId());
} else { } else {
return false; return false;
} }
@ -1157,8 +1157,8 @@ public class SlimefunItem implements Placeable {
return null; return null;
} }
if (item instanceof SlimefunItemStack) { if (item instanceof SlimefunItemStack stack) {
return getById(((SlimefunItemStack) item).getItemId()); return getById(stack.getItemId());
} }
Optional<String> itemID = Slimefun.getItemDataService().getItemData(item); Optional<String> itemID = Slimefun.getItemDataService().getItemData(item);

View File

@ -517,7 +517,7 @@ public class PlayerProfile {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
return obj instanceof PlayerProfile && uuid.equals(((PlayerProfile) obj).uuid); return obj instanceof PlayerProfile profile && uuid.equals(profile.uuid);
} }
@Override @Override

View File

@ -146,7 +146,7 @@ class VersionsCommand extends SubCommand {
secondaryColor = ChatColor.DARK_GREEN; secondaryColor = ChatColor.DARK_GREEN;
String authors = String.join(", ", plugin.getDescription().getAuthors()); String authors = String.join(", ", plugin.getDescription().getAuthors());
if (plugin instanceof SlimefunAddon && ((SlimefunAddon) plugin).getBugTrackerURL() != null) { if (plugin instanceof SlimefunAddon addon && addon.getBugTrackerURL() != null) {
// @formatter:off // @formatter:off
hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(new ComponentBuilder() hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(new ComponentBuilder()
.append("Author(s): ") .append("Author(s): ")
@ -158,7 +158,7 @@ class VersionsCommand extends SubCommand {
)); ));
// @formatter:on // @formatter:on
clickEvent = new ClickEvent(ClickEvent.Action.OPEN_URL, ((SlimefunAddon) plugin).getBugTrackerURL()); clickEvent = new ClickEvent(ClickEvent.Action.OPEN_URL, addon.getBugTrackerURL());
} else { } else {
// @formatter:off // @formatter:off
hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(new ComponentBuilder() hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(new ComponentBuilder()
@ -173,7 +173,7 @@ class VersionsCommand extends SubCommand {
primaryColor = ChatColor.RED; primaryColor = ChatColor.RED;
secondaryColor = ChatColor.DARK_RED; secondaryColor = ChatColor.DARK_RED;
if (plugin instanceof SlimefunAddon && ((SlimefunAddon) plugin).getBugTrackerURL() != null) { if (plugin instanceof SlimefunAddon addon && addon.getBugTrackerURL() != null) {
// @formatter:off // @formatter:off
hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(new ComponentBuilder() hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(new ComponentBuilder()
.append("This plugin is disabled.\nCheck the console for an error message.") .append("This plugin is disabled.\nCheck the console for an error message.")
@ -184,8 +184,6 @@ class VersionsCommand extends SubCommand {
)); ));
// @formatter:on // @formatter:on
SlimefunAddon addon = (SlimefunAddon) plugin;
if (addon.getBugTrackerURL() != null) { if (addon.getBugTrackerURL() != null) {
clickEvent = new ClickEvent(ClickEvent.Action.OPEN_URL, addon.getBugTrackerURL()); clickEvent = new ClickEvent(ClickEvent.Action.OPEN_URL, addon.getBugTrackerURL());
} }

View File

@ -188,14 +188,14 @@ public class GuideHistory {
private <T> void open(@Nonnull SlimefunGuideImplementation guide, @Nullable GuideEntry<T> entry) { private <T> void open(@Nonnull SlimefunGuideImplementation guide, @Nullable GuideEntry<T> entry) {
if (entry == null) { if (entry == null) {
guide.openMainMenu(profile, mainMenuPage); guide.openMainMenu(profile, mainMenuPage);
} else if (entry.getIndexedObject() instanceof ItemGroup) { } else if (entry.getIndexedObject() instanceof ItemGroup group) {
guide.openItemGroup(profile, (ItemGroup) entry.getIndexedObject(), entry.getPage()); guide.openItemGroup(profile, group, entry.getPage());
} else if (entry.getIndexedObject() instanceof SlimefunItem) { } else if (entry.getIndexedObject() instanceof SlimefunItem item) {
guide.displayItem(profile, (SlimefunItem) entry.getIndexedObject(), false); guide.displayItem(profile, item, false);
} else if (entry.getIndexedObject() instanceof ItemStack) { } else if (entry.getIndexedObject() instanceof ItemStack stack) {
guide.displayItem(profile, (ItemStack) entry.getIndexedObject(), entry.getPage(), false); guide.displayItem(profile, stack, entry.getPage(), false);
} else if (entry.getIndexedObject() instanceof String) { } else if (entry.getIndexedObject() instanceof String query) {
guide.openSearch(profile, (String) entry.getIndexedObject(), false); guide.openSearch(profile, query, false);
} else { } else {
throw new IllegalStateException("Unknown GuideHistory entry: " + entry.getIndexedObject()); throw new IllegalStateException("Unknown GuideHistory entry: " + entry.getIndexedObject());
} }

View File

@ -99,8 +99,7 @@ public class MinecraftRecipeService {
* *
* @return An {@link Optional} describing the furnace output of the given {@link ItemStack} * @return An {@link Optional} describing the furnace output of the given {@link ItemStack}
*/ */
@Nonnull public @Nonnull Optional<ItemStack> getFurnaceOutput(@Nullable ItemStack input) {
public Optional<ItemStack> getFurnaceOutput(@Nullable ItemStack input) {
if (snapshot == null || input == null) { if (snapshot == null || input == null) {
return Optional.empty(); return Optional.empty();
} }
@ -132,18 +131,17 @@ public class MinecraftRecipeService {
* *
* @return An Array of {@link RecipeChoice} representing the shape of this {@link Recipe} * @return An Array of {@link RecipeChoice} representing the shape of this {@link Recipe}
*/ */
@Nonnull public @Nonnull RecipeChoice[] getRecipeShape(@Nonnull Recipe recipe) {
public RecipeChoice[] getRecipeShape(@Nonnull Recipe recipe) {
Validate.notNull(recipe, "Recipe must not be null!"); Validate.notNull(recipe, "Recipe must not be null!");
if (recipe instanceof ShapedRecipe) { if (recipe instanceof ShapedRecipe shapedRecipe) {
List<RecipeChoice> choices = new LinkedList<>(); List<RecipeChoice> choices = new LinkedList<>();
for (String row : ((ShapedRecipe) recipe).getShape()) { for (String row : shapedRecipe.getShape()) {
int columns = row.toCharArray().length; int columns = row.toCharArray().length;
for (char key : row.toCharArray()) { for (char key : row.toCharArray()) {
choices.add(((ShapedRecipe) recipe).getChoiceMap().get(key)); choices.add(shapedRecipe.getChoiceMap().get(key));
} }
while (columns < 3) { while (columns < 3) {
@ -167,8 +165,7 @@ public class MinecraftRecipeService {
* *
* @return An array of {@link Recipe Recipes} to craft the given {@link ItemStack} * @return An array of {@link Recipe Recipes} to craft the given {@link ItemStack}
*/ */
@Nonnull public @Nonnull Recipe[] getRecipesFor(@Nullable ItemStack item) {
public Recipe[] getRecipesFor(@Nullable ItemStack item) {
if (snapshot == null || item == null) { if (snapshot == null || item == null) {
return new Recipe[0]; return new Recipe[0];
} else { } else {
@ -187,8 +184,7 @@ public class MinecraftRecipeService {
* *
* @return The corresponding {@link Recipe} or null * @return The corresponding {@link Recipe} or null
*/ */
@Nullable public @Nullable Recipe getRecipe(@Nonnull NamespacedKey key) {
public Recipe getRecipe(@Nonnull NamespacedKey key) {
Validate.notNull(key, "The NamespacedKey should not be null"); Validate.notNull(key, "The NamespacedKey should not be null");
if (snapshot != null) { if (snapshot != null) {