mirror of
https://github.com/StarWishsama/Slimefun4.git
synced 2024-09-20 03:35:51 +00:00
Impelemented a few instanceof patterns, actually quite nice :)
This commit is contained in:
parent
20c3564e8f
commit
bb9e2dfa4a
@ -327,8 +327,8 @@ public class ItemGroup implements Keyed {
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
if (obj instanceof ItemGroup) {
|
||||
return ((ItemGroup) obj).getKey().equals(getKey());
|
||||
if (obj instanceof ItemGroup group) {
|
||||
return group.getKey().equals(this.getKey());
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
@ -482,8 +482,8 @@ public class SlimefunItem implements Placeable {
|
||||
}
|
||||
|
||||
// Lock the SlimefunItemStack from any accidental manipulations
|
||||
if (itemStackTemplate instanceof SlimefunItemStack && isItemStackImmutable()) {
|
||||
((SlimefunItemStack) itemStackTemplate).lock();
|
||||
if (itemStackTemplate instanceof SlimefunItemStack stack && isItemStackImmutable()) {
|
||||
stack.lock();
|
||||
}
|
||||
|
||||
postRegister();
|
||||
@ -755,8 +755,8 @@ public class SlimefunItem implements Placeable {
|
||||
}
|
||||
|
||||
// If the given item is a SlimefunitemStack, simply compare the id
|
||||
if (item instanceof SlimefunItemStack) {
|
||||
return getId().equals(((SlimefunItemStack) item).getItemId());
|
||||
if (item instanceof SlimefunItemStack stack) {
|
||||
return getId().equals(stack.getItemId());
|
||||
}
|
||||
|
||||
if (item.hasItemMeta()) {
|
||||
@ -807,10 +807,10 @@ public class SlimefunItem implements Placeable {
|
||||
itemhandlers.put(handler.getIdentifier(), handler);
|
||||
|
||||
// Tickers are a special case (at the moment at least)
|
||||
if (handler instanceof BlockTicker) {
|
||||
if (handler instanceof BlockTicker ticker) {
|
||||
ticking = true;
|
||||
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);
|
||||
|
||||
// We definitely want to re-throw them during Unit Tests
|
||||
if (throwable instanceof RuntimeException && Slimefun.getMinecraftVersion() == MinecraftVersion.UNIT_TEST) {
|
||||
throw (RuntimeException) throwable;
|
||||
if (throwable instanceof RuntimeException e && Slimefun.getMinecraftVersion() == MinecraftVersion.UNIT_TEST) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1122,8 +1122,8 @@ public class SlimefunItem implements Placeable {
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
if (obj instanceof SlimefunItem) {
|
||||
return ((SlimefunItem) obj).getId().equals(getId());
|
||||
if (obj instanceof SlimefunItem item) {
|
||||
return item.getId().equals(this.getId());
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
@ -1157,8 +1157,8 @@ public class SlimefunItem implements Placeable {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (item instanceof SlimefunItemStack) {
|
||||
return getById(((SlimefunItemStack) item).getItemId());
|
||||
if (item instanceof SlimefunItemStack stack) {
|
||||
return getById(stack.getItemId());
|
||||
}
|
||||
|
||||
Optional<String> itemID = Slimefun.getItemDataService().getItemData(item);
|
||||
|
@ -517,7 +517,7 @@ public class PlayerProfile {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return obj instanceof PlayerProfile && uuid.equals(((PlayerProfile) obj).uuid);
|
||||
return obj instanceof PlayerProfile profile && uuid.equals(profile.uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -146,7 +146,7 @@ class VersionsCommand extends SubCommand {
|
||||
secondaryColor = ChatColor.DARK_GREEN;
|
||||
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
|
||||
hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(new ComponentBuilder()
|
||||
.append("Author(s): ")
|
||||
@ -158,7 +158,7 @@ class VersionsCommand extends SubCommand {
|
||||
));
|
||||
// @formatter:on
|
||||
|
||||
clickEvent = new ClickEvent(ClickEvent.Action.OPEN_URL, ((SlimefunAddon) plugin).getBugTrackerURL());
|
||||
clickEvent = new ClickEvent(ClickEvent.Action.OPEN_URL, addon.getBugTrackerURL());
|
||||
} else {
|
||||
// @formatter:off
|
||||
hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(new ComponentBuilder()
|
||||
@ -173,7 +173,7 @@ class VersionsCommand extends SubCommand {
|
||||
primaryColor = ChatColor.RED;
|
||||
secondaryColor = ChatColor.DARK_RED;
|
||||
|
||||
if (plugin instanceof SlimefunAddon && ((SlimefunAddon) plugin).getBugTrackerURL() != null) {
|
||||
if (plugin instanceof SlimefunAddon addon && addon.getBugTrackerURL() != null) {
|
||||
// @formatter:off
|
||||
hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(new ComponentBuilder()
|
||||
.append("This plugin is disabled.\nCheck the console for an error message.")
|
||||
@ -184,8 +184,6 @@ class VersionsCommand extends SubCommand {
|
||||
));
|
||||
// @formatter:on
|
||||
|
||||
SlimefunAddon addon = (SlimefunAddon) plugin;
|
||||
|
||||
if (addon.getBugTrackerURL() != null) {
|
||||
clickEvent = new ClickEvent(ClickEvent.Action.OPEN_URL, addon.getBugTrackerURL());
|
||||
}
|
||||
|
@ -188,14 +188,14 @@ public class GuideHistory {
|
||||
private <T> void open(@Nonnull SlimefunGuideImplementation guide, @Nullable GuideEntry<T> entry) {
|
||||
if (entry == null) {
|
||||
guide.openMainMenu(profile, mainMenuPage);
|
||||
} else if (entry.getIndexedObject() instanceof ItemGroup) {
|
||||
guide.openItemGroup(profile, (ItemGroup) entry.getIndexedObject(), entry.getPage());
|
||||
} else if (entry.getIndexedObject() instanceof SlimefunItem) {
|
||||
guide.displayItem(profile, (SlimefunItem) entry.getIndexedObject(), false);
|
||||
} else if (entry.getIndexedObject() instanceof ItemStack) {
|
||||
guide.displayItem(profile, (ItemStack) entry.getIndexedObject(), entry.getPage(), false);
|
||||
} else if (entry.getIndexedObject() instanceof String) {
|
||||
guide.openSearch(profile, (String) entry.getIndexedObject(), false);
|
||||
} else if (entry.getIndexedObject() instanceof ItemGroup group) {
|
||||
guide.openItemGroup(profile, group, entry.getPage());
|
||||
} else if (entry.getIndexedObject() instanceof SlimefunItem item) {
|
||||
guide.displayItem(profile, item, false);
|
||||
} else if (entry.getIndexedObject() instanceof ItemStack stack) {
|
||||
guide.displayItem(profile, stack, entry.getPage(), false);
|
||||
} else if (entry.getIndexedObject() instanceof String query) {
|
||||
guide.openSearch(profile, query, false);
|
||||
} else {
|
||||
throw new IllegalStateException("Unknown GuideHistory entry: " + entry.getIndexedObject());
|
||||
}
|
||||
|
@ -99,8 +99,7 @@ public class MinecraftRecipeService {
|
||||
*
|
||||
* @return An {@link Optional} describing the furnace output of the given {@link ItemStack}
|
||||
*/
|
||||
@Nonnull
|
||||
public Optional<ItemStack> getFurnaceOutput(@Nullable ItemStack input) {
|
||||
public @Nonnull Optional<ItemStack> getFurnaceOutput(@Nullable ItemStack input) {
|
||||
if (snapshot == null || input == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
@ -132,18 +131,17 @@ public class MinecraftRecipeService {
|
||||
*
|
||||
* @return An Array of {@link RecipeChoice} representing the shape of this {@link Recipe}
|
||||
*/
|
||||
@Nonnull
|
||||
public RecipeChoice[] getRecipeShape(@Nonnull Recipe recipe) {
|
||||
public @Nonnull RecipeChoice[] getRecipeShape(@Nonnull Recipe recipe) {
|
||||
Validate.notNull(recipe, "Recipe must not be null!");
|
||||
|
||||
if (recipe instanceof ShapedRecipe) {
|
||||
if (recipe instanceof ShapedRecipe shapedRecipe) {
|
||||
List<RecipeChoice> choices = new LinkedList<>();
|
||||
|
||||
for (String row : ((ShapedRecipe) recipe).getShape()) {
|
||||
for (String row : shapedRecipe.getShape()) {
|
||||
int columns = row.toCharArray().length;
|
||||
|
||||
for (char key : row.toCharArray()) {
|
||||
choices.add(((ShapedRecipe) recipe).getChoiceMap().get(key));
|
||||
choices.add(shapedRecipe.getChoiceMap().get(key));
|
||||
}
|
||||
|
||||
while (columns < 3) {
|
||||
@ -167,8 +165,7 @@ public class MinecraftRecipeService {
|
||||
*
|
||||
* @return An array of {@link Recipe Recipes} to craft the given {@link ItemStack}
|
||||
*/
|
||||
@Nonnull
|
||||
public Recipe[] getRecipesFor(@Nullable ItemStack item) {
|
||||
public @Nonnull Recipe[] getRecipesFor(@Nullable ItemStack item) {
|
||||
if (snapshot == null || item == null) {
|
||||
return new Recipe[0];
|
||||
} else {
|
||||
@ -187,8 +184,7 @@ public class MinecraftRecipeService {
|
||||
*
|
||||
* @return The corresponding {@link Recipe} or null
|
||||
*/
|
||||
@Nullable
|
||||
public Recipe getRecipe(@Nonnull NamespacedKey key) {
|
||||
public @Nullable Recipe getRecipe(@Nonnull NamespacedKey key) {
|
||||
Validate.notNull(key, "The NamespacedKey should not be null");
|
||||
|
||||
if (snapshot != null) {
|
||||
|
Loading…
Reference in New Issue
Block a user