Solved Save the color of the block (Glass & Concrete) [1.12.2]

Discussion in 'Plugin Development' started by Synchroneyes, Nov 5, 2019.

  1. Offline

    Synchroneyes

    Hello!
    I'm trying to make a class called "DisplayBlock".
    This class basically hide & display a block.
    Code:
    public class DisplayBlock {
        private Block baseBlock;
        private Location position;
        private Material materiel;
        private BlockState etat;
    
        public DisplayBlock(Block baseBlock) {
    
            try {
                this.baseBlock = baseBlock;
                this.position = baseBlock.getLocation();
                this.materiel = baseBlock.getState().getType();
                this.etat = baseBlock.getState();
            }catch (Exception e){
                e.printStackTrace();
            }
    
    
        }
    
    
        public Block getBlock() { return baseBlock;}
    
        public void display() {
    
            position.getBlock().setType(materiel);
    
    
        }
        public void hide() {
    
            position.getBlock().setType(Material.AIR);
    
        }
    }
    I'm able to hide & display the block but the block lose its color when i'm displaying it.
    My question is, how can i set the block color to its original one ?
    I'm running bukkit in 1.12.2
     
  2. Offline

    KarimAKL

    @Synchroneyes In versions below 1.13, you have to set the data as well as the material. You can see the data for wool colors here.
     
  3. Offline

    Synchroneyes

    Thank you for your answer, but i'm not working with wool :/

    In the constructor, i'm storing the state of the block, and when i print the type of the state, i successfully get Concrete(COLOR ID) in the console. But how can i apply it to my concrete block or glass ?
     
  4. Offline

    KarimAKL

    Glass and Concrete use the same values for the colors as wool does, you can view them all here:
    Wool: https://minecraft.gamepedia.com/Wool#Block_data
    Glass: https://minecraft.gamepedia.com/Glass#Block_data
    Concrete: https://minecraft.gamepedia.com/Concrete#Block_data

    After looking through the javadocs for different versions, i think there's multiple ways to do it but, it seems like this would work for 1.13+ and <1.13:
    Code:Java
    1. block.getState().setData(block.getState().getData());

    Though i haven't tested it.
     
  5. Offline

    Synchroneyes

    Thank you for your answer. I tried to do

    Code:
    public void display() {
    
            position.getBlock().getState().setData(etat.getData());
            position.getBlock().getState().update();
            mainPlugin.plugin.getServer().broadcastMessage(position.getBlock().getState().getData().toString());
    
    
        }
    but nothing is showing back, and the game is printing AIR(0)

    -----------------------------------------

    Updating to 1.14.4 and using this code solved my problem
    Code:
    position.getBlock().setType(materiel);
                position.getBlock().setBlockData(baseBlock.getBlockData());
                position.getBlock().getState().setData(this.data);
    
    
    
                position.getBlock().getState().update();
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Nov 6, 2019
  6. Offline

    KarimAKL

    @Synchroneyes May i ask why you didn't update to 1.14.4 to begin with? Glad you solved it though.
     
  7. Offline

    Synchroneyes

    I dont know ehe, when i started working on my plugin my friend told me to use 1.12.2, so i've followed his advice :)
     

Share This Page