Solved Connecting double chests 1.13.1?

Discussion in 'Plugin Development' started by bowlerguy66, Oct 13, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    bowlerguy66

    I'm trying to create double chests and put items in them, but instead of creating a double chest I get two single chest blocks directly adjacent to each other. Any ideas?

    Code:
                    Block leftSide = targ.getBlock();
                    Block rightSide = targ.clone().add(0, 0, -1).getBlock();
                   
                    leftSide.setType(Material.CHEST);
                    rightSide.setType(Material.CHEST);
                                   
                    BlockData leftData = leftSide.getBlockData();
                    ((Directional) leftData).setFacing(BlockFace.EAST);
                    leftSide.setBlockData(leftData);
                    Chest leftChest = (Chest) leftSide.getState();       
                                   
                    BlockData rightData = rightSide.getBlockData();
                    ((Directional) rightData).setFacing(BlockFace.EAST);
                    rightSide.setBlockData(rightData);
                    Chest rightChest = (Chest) rightSide.getState();       
                   
                    leftSide.getState().update(true, true);
                    rightSide.getState().update(true, true);
    
     
  2. Have you put all this code in a method/constructor or a event handler and if so what event?

    Or, please send the full code of that class

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 13, 2018
  3. Offline

    bowlerguy66

    @Shadow_tingBRO
    Code:
    package me.bowlerguy66.prison.auctionhouse;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.BlockFace;
    import org.bukkit.block.Chest;
    import org.bukkit.block.data.BlockData;
    import org.bukkit.block.data.Directional;
    import org.bukkit.inventory.ItemStack;
    
    import me.bowlerguy66.prison.Prison;
    
    public class AuctionHouseManager {
    
        @SuppressWarnings("unused")
        private Prison plugin;
       
        public AuctionHouseManager(Prison instance) {
            this.plugin = instance;
        }
       
        public void addChests(List<ItemStack> items) {
           
            Location rowOneAnchor = new Location(Bukkit.getWorld(Prison.mainWorldName), 31, 66, 6);
                   
            for(int y = rowOneAnchor.getBlockY(); y < rowOneAnchor.getBlockY() + 4; y++) {
                for(int xBase = rowOneAnchor.getBlockX(); xBase < rowOneAnchor.getBlockX() + 10; xBase++) {
                   
                    if(items.isEmpty()) {
                        return;
                    }
                   
                    int x = xBase;
                   
                    if(x > rowOneAnchor.getBlockX() + 4) {
                        x = x + 2;
                    }
                   
                    Location targ = new Location(rowOneAnchor.getWorld(), x, y, rowOneAnchor.getBlockZ());
                   
                    if(targ.getBlock().getType() == Material.CHEST) {
                        continue;
                    }
                   
                    Block leftSide = targ.getBlock();
                    Block rightSide = targ.clone().add(0, 0, -1).getBlock();
                   
                    leftSide.setType(Material.CHEST);
                    rightSide.setType(Material.CHEST);
                                   
                    BlockData leftData = leftSide.getBlockData();
                    ((Directional) leftData).setFacing(BlockFace.EAST);
                    leftSide.setBlockData(leftData);
                    Chest leftChest = (Chest) leftSide.getState();       
                                   
                    BlockData rightData = rightSide.getBlockData();
                    ((Directional) rightData).setFacing(BlockFace.EAST);
                    rightSide.setBlockData(rightData);
                    Chest rightChest = (Chest) rightSide.getState();       
    
    //                leftChest.
                   
                    leftSide.getState().update(true, true);
                    rightSide.getState().update(true, true);
                   
                    List<ItemStack> toRemove = new ArrayList<ItemStack>();
                   
                    for(ItemStack i : items) {
                        leftChest.getInventory().addItem(i);
                        toRemove.add(i);
                    }
                   
                    for(ItemStack i : toRemove) {
                        items.remove(i);
                    }
                   
                }
            }
           
        }
       
    }
    
     
  4. Offline

    The_Spaceman

    what about this:

    Code:java
    1. org.bukkit.block.data.type.Chest chestDataLeft = (Chest) leftSide.getBlockData();
    2. org.bukkit.block.data.type.Chest chestDataRight = (Chest) rightSide.getBlockData();
    3.  
    4. chestDataLeft.setType(org.bukkit.block.data.type.Chest.Type.LEFT);
    5. chestDataRight.setType(org.bukkit.block.data.type.Chest.Type.RIGHT);

    I have no idea if this works... just try
     
  5. Offline

    bowlerguy66

    That didn't work. Anything else?
     
  6. Offline

    Esophose

    You have to create the block data manually and then apply it to the chests. Depending on what direction you want the chests to face, you'll have to edit facing, type, and the coordinates.
    Code:
    Block leftSide = targ.getBlock();
    Block rightSide = targ.clone().add(0, 0, -1).getBlock();
         
    leftSide.setType(Material.CHEST);
    rightSide.setType(Material.CHEST);
         
    leftSide.setBlockData(Material.CHEST.createBlockData("[facing=east,type=right]"));
    rightSide.setBlockData(Material.CHEST.createBlockData("[facing=east,type=left]"));
    Yes, I do realize that the leftSide is getting set as type=right and that rightSide is getting set as type=left, it doesn't work the other way around. I'm not exactly sure how this all works, but that's how you do it.

    Alternatively, you can also do it like this in a more API-like fashion:
    Code:
    Block leftSide = targ.getBlock();
    Block rightSide = targ.clone().add(0, 0, -1).getBlock();
           
    leftSide.setType(Material.CHEST);
    rightSide.setType(Material.CHEST);
           
    Chest leftChestData = (Chest)leftSide.getBlockData();
    Chest rightChestData = (Chest)rightSide.getBlockData();
           
    leftChestData.setFacing(BlockFace.EAST);
    leftChestData.setType(Type.RIGHT);
           
    rightChestData.setFacing(BlockFace.EAST);
    rightChestData.setType(Type.LEFT);
           
    leftSide.setBlockData(leftChestData);
    rightSide.setBlockData(rightChestData);
     
    Last edited: Oct 13, 2018
  7. Offline

    bowlerguy66

    Actually, I got it working by switching the left and right, thanks you guys.

    Finished code (open)
    Code:
                    Block leftSide = targ.getBlock();
                    Block rightSide = targ.clone().add(0, 0, -1).getBlock();
                   
                    leftSide.setType(Material.CHEST);
                    rightSide.setType(Material.CHEST);
                                   
                    BlockData leftData = leftSide.getBlockData();
                    ((Directional) leftData).setFacing(BlockFace.EAST);
                    leftSide.setBlockData(leftData);
    
                    org.bukkit.block.data.type.Chest chestDataLeft = (org.bukkit.block.data.type.Chest) leftData;
                    chestDataLeft.setType(Type.RIGHT);
                    leftSide.setBlockData(chestDataLeft);
    
                    Chest leftChest = (Chest) leftSide.getState();       
                                   
                    BlockData rightData = rightSide.getBlockData();
                    ((Directional) rightData).setFacing(BlockFace.EAST);
                    rightSide.setBlockData(rightData);
               
                    org.bukkit.block.data.type.Chest chestDataRight = (org.bukkit.block.data.type.Chest) rightData;
                    chestDataRight.setType(Type.LEFT);
                    rightSide.setBlockData(chestDataRight);
    


    Hopefully they make this more efficient later lol
     
Thread Status:
Not open for further replies.

Share This Page