Jump to content

[SOLVED][1.7.2]Eclipse wants me to cast my class to itself?


jabelar

Recommended Posts

Sorry this is more of a Java question.  But I have a custom item called ItemGoldenEgg that extends Item.  I'm trying to create a new one and assign it to a static instance field.  Anyway, Eclipse warns me that ItemGoldenEgg is an Item and needs to be cast into ItemGoldenEgg.  I don't understand why I would ever have to cast something as itself, but I'm weak on some Java concepts..

 

Anyway here is the line of code (it complains if I don't have the cast in bold below):

        MagicBeans.itemGoldenEgg = (ItemGoldenEgg) new ItemGoldenEgg().setUnlocalizedName("golden_egg").setTextureName("magicbeans:golden_egg");

 

And the declaration for the instance itemGoldenEgg is in my main class and is:

    public static ItemGoldenEgg itemGoldenEgg;

 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Sorry this is more of a Java question.  But I have a custom item called ItemGoldenEgg that extends Item.  I'm trying to create a new one and assign it to a static instance field.  Anyway, Eclipse warns me that ItemGoldenEgg is an Item and needs to be cast into ItemGoldenEgg.  I don't understand why I would ever have to cast something as itself, but I'm weak on some Java concepts..

 

Anyway here is the line of code (it complains if I don't have the cast in bold below):

        MagicBeans.itemGoldenEgg = (ItemGoldenEgg) new ItemGoldenEgg().setUnlocalizedName("golden_egg").setTextureName("magicbeans:golden_egg");

 

And the declaration for the instance itemGoldenEgg is in my main class and is:

    public static ItemGoldenEgg itemGoldenEgg;

 

Hello Jabelar!

Java does not want you to cast your ItemGoldenEgg to itself, it wants you to cast an Item to ItemGoldenEgg. What you are assigning to MagicBeans.itemGoldenEgg is not the object returned by your constructor, but rather the object returned by setTextureName(), which is the last method in that line, therefore what is returned by it is assigned to your variable. If you look at the definition for setTextureName(String par1Str), you can see that it returns an Item object. Java automatically casts object to their superclasses, like so:

Item randomItem = new ItemGoldenEgg(); 

but you need to manually cast it from a superclass to one of the inheriting classes, like so:

ItemGoldenEgg randomItem = (ItemGoldenEgg)new ItemGoldenEgg().setTextureName("TEXTURE");

 

Also, on a side note, you should probably just make the variable an Item, not an ItemGoldenEgg, and then cast it to your own class when you need to call your own methods.

Item itemGoldenEgg = new ItemGoldenEgg().setUnlocalizedName("golden_egg").setTextureName("magicbeans:golden_egg");
(ItemGoldenEgg)itemGoldenEgg.hatchEgg();

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • this is my first time using this website. Im having an issue where if I have EC Apotheosis enabled the game launches but crashes on the loading screen. If I disable the mod the game runs fine. Dont know if it will help but this is the exported curseforge proflie file. https://drive.google.com/file/d/1iDoqIGmiJsTKW5ocru7FBxIdBiKgUr0i/view?usp=sharing
    • How to fix file server-1.20.1-20230612.114412-srg.jar  
    • Just a few months ago I was creating my own plugin for Minecraft 1.20.2 spigot that did the same thing, but the skins were not saved, if you can understand this code that I made a long time ago it may help you.   //This is a class method private static String APIRequest(String value, String url, String toSearch) { try { URL api = new URL(url + value); HttpURLConnection connection = (HttpURLConnection) api.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder response = new StringBuilder(); for (String responseChar; (responseChar = reader.readLine()) != null; ) response.append(responseChar); reader.close(); JSONObject responseObject = new JSONObject(response.toString()); if (!toSearch.equals("id")) return responseObject .getJSONArray("properties") .getJSONObject(0) .getString("value"); else return responseObject.getString("id"); } else { AntiGianka.ConsoleMessage(ChatColor.RED, String.format( "Could not get %s. Response code: %s", ((toSearch.equals("id")) ? "UUID" : "texture"), responseCode )); } } catch (MalformedURLException error) { AntiGianka.ConsoleMessage(ChatColor.RED, "An error occurred while trying to access the URL. Error: " + error); } catch (IOException error) { AntiGianka.ConsoleMessage(ChatColor.RED, "An error occurred while attempting to connect to the URL. Error: " + error); } return ""; } //other class method private void SkinGetter() { String uuid; String textureCoded; if ((uuid = APIRequest(args[0], "https://api.mojang.com/users/profiles/minecraft/", "id")).isEmpty() || (textureCoded = APIRequest(uuid, "https://sessionserver.mojang.com/session/minecraft/profile/", "value")).isEmpty() ) sender.sendMessage(ChatColor.RED + String.format( "An error has occurred while trying to obtain the %s player skin, please check if the name %s is spelled correctly or try again later.", args[0], args[0] ) ); else SkinSetter(textureCoded); } //other more private void SkinSetter(String textureCoded) { JSONObject profile = new JSONObject(new String(Base64.getDecoder().decode(textureCoded))); try { URL textureUrl = new URL(profile.getJSONObject("textures"). getJSONObject("SKIN"). getString("url")); if (sender instanceof Player && args.length == 1) { PlayerTextures playerTextures = ((Player) sender).getPlayerProfile().getTextures(); playerTextures.setSkin(textureUrl); ((Player) sender).getPlayerProfile().setTextures(playerTextures); if (((Player) sender).getPlayerProfile().getTextures().getSkin() != null) sender.sendMessage(((Player) sender).getPlayerProfile().getTextures().getSkin().toString()); else sender.sendMessage("Null"); sender.sendMessage("Skin changed successfully.a"); } else { } AntiGianka.ConsoleMessage(ChatColor.GREEN, "Skin command executed successfully."); } catch (MalformedURLException error) { sender.sendMessage(ChatColor.RED + String.format( "An error has occurred while trying to obtain the %s player skin, please check if the name %s is spelled correctly or try again later.", args[0], args[0] ) ); AntiGianka.ConsoleMessage(ChatColor.RED, "An error occurred while trying to access the URL. Error: " + error); } }  
    • Use /locate structure The chat should show all available structures as suggestion For the Ancient City it is /locate structure ancient_city
    • So does it work without this mod? Did you test it with other builds?
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.