Solved Referencing a Variable in Multiple Classes

Discussion in 'Spigot Plugin Help' started by Joto_is_here, Jul 14, 2021.

  1. hello! I've been having a lot of trouble with this code I've been working on, I've been trying to make a "Tool Cupboard" like in rust and I'm not quite sure what's going wrong. any help would be nice, thanks!
    Code (Text):
    public static ItemStack item;
        public static Chunk chunk;
        public static Player TCPlacer;

        public static void TC(){
            item = new ItemStack(Material.CHEST);
             ItemMeta meta = item.getItemMeta();
            meta.setDisplayName("TC");
            item.setItemMeta(meta);
            meta.setLore(Arrays.asList(ChatColor.GOLD + "Keeps Your Base Alive!"));
            NamespacedKey key = new NamespacedKey(JavaPlugin.getPlugin(Main.class), "TC");
            ShapedRecipe recipe = new ShapedRecipe(key,item);
            recipe.shape("AWA","AAA","WWW");
            recipe.setIngredient('W',Material.OAK_WOOD);
            recipe.setIngredient('A',Material.IRON_INGOT);
            Bukkit.addRecipe(recipe);

        }
        @EventHandler
        public static void Blockplace(BlockPlaceEvent e){
            if(e.getBlockPlaced().equals(item)){
               TCPlacer = e.getPlayer();
                chunk = e.getBlockPlaced().getChunk();
            }
        }
        @EventHandler
        public static void BlockBreak(BlockBreakEvent e){
            if(e.getPlayer().getLocation().equals(chunk))
            if(!TCPlacer.equals(e.getPlayer())){
            e.setCancelled(true);
            }
        }
    }

     
     
  2. I recommend reading Java Naming Conventions

    Looking a bit at your code, see that you need to specify the ItemStack variable. Even if I do it above it seems strange, it would be nice to paste the whole class

    Code (Java):
    ItemStack item = new ItemStack(Material.CHEST);
    Then you are setting the itemMeta before setting the lore so that's wrong.
    Use the item#setItemMeta(meta) method when you finish "setting" the item
    Code (Java):

    item = new ItemStack(Material.CHEST);
    ItemMeta meta = item.getItemMeta();
    meta.setDisplayName("TC");
    item.setItemMeta(meta); //THIS IS WRONG! Delete it
    meta.setLore(Arrays.asList(ChatColor.GOLD + "Keeps Your Base Alive!"));
    NamespacedKey key = new NamespacedKey(JavaPlugin.getPlugin(Main.class), "TC");
    ShapedRecipe recipe = new ShapedRecipe(key,item);
    recipe.shape("AWA","AAA","WWW");
    recipe.setIngredient('W',Material.OAK_WOOD);
    recipe.setIngredient('A',Material.IRON_INGOT);
    Bukkit.addRecipe(recipe);
    item.setItemMeta(meta); //this is functional
    You can't compare player#getLocation with Chunk
    Try this
    Code (Java):
     if (player.getLocation().getChunk().equals(chunk)){
        //...
      }
    Same for e#getBlockPlaced with item
    Try this
    Code (Java):
    if (e.getBlockPlaced().getType() == item.getType()){
        //...
      }
    Then I see unnecessary things like comparing a player with the same player :unsure:
    Try to fix that, maybe you have a better idea of what you are doing in your code :coffee:
     
  3. Why use static to use events? Also, you are doing static abuse. Don't use static.
     
  4. Generally a very object-oriented approach to sharing variables is to use "dependency injection" (or DI for short).
    In its simplest form you just pass the value as an argument to the constructor of the classes that need the variable.

    In your Main/onEnable:
    Code (Java):
    MyClass myVariable = //create the variable

    ConsumerOne consumerOne = new ConsumerOne(myVariable);
    ConsumerTwo consumerTwo = new ConsumerTwo(myVariable);
    ConsumerOne:
    Code (Java):
    public class ConsumerOne {
        private final MyClass theVariable;

        public ConsumerOne(MyClass variable) {
            this.theVariable = variable;
        }
        //use theVariable in other methods of this class. It'll be the same value as in ConsumerTwo.
    }
     
    ConsumerTwo:
    Code (Java):
    public class ConsumerTwo {
        private final MyClass theVariable;

        public ConsumerTwo(MyClass variable) {
            this.theVariable = variable;
        }
        //use theVariable in other methods of this class. It'll be the same value as in ConsumerOne.
    }
     
    #4 Jannyboy11, Jul 14, 2021
    Last edited: Jul 14, 2021
    • Agree Agree x 1