Jump to content

[1.8.9] [SOLVED] UnlistedProperty return null


feldim2425

Recommended Posts

I have a MCMultipart cable. I can write the 6 directions to the Extendet Blockstate without problem, but if I get the Property Value, the function returns null.

 

Cable Class

public class PartRfCable extends Multipart implements IOccludingPart, ISlottedPart, ITickable, IEnergyHandler {

    public static IUnlistedProperty<Boolean>[] sides = PropertyCableConnection.getPropertyList();
    public static IProperty<EnumCableType> cableType = new EnumCableType.PropertyCableType();

    private HashMap<EnumFacing, Boolean> connections = new HashMap<>();
    private boolean updateDone;
    private EnumCableType type;

    public PartRfCable(EnumCableType type){
        super();
        this.type=type;
    }

    public PartRfCable(){
        super();
    }

    @Override
    public void addOcclusionBoxes(List<AxisAlignedBB> list) {
        list.add(AxisAlignedBB.fromBounds(0,0,0,1,1,1));
    }

    @Override
    public void addSelectionBoxes(List<AxisAlignedBB> list) {
        list.add(AxisAlignedBB.fromBounds(4F/16F,4F/16F,4F/16F,12F/16F,12F/16F,12F/16F));
    }

    @Override
    public void addCollisionBoxes(AxisAlignedBB mask, List<AxisAlignedBB> list, Entity collidingEntity) {
        list.add(AxisAlignedBB.fromBounds(0,0,0,1,1,1));
    }

    @Override
    public void update() {
        if(!updateDone){   //onLoaded() gets called before other TE's are loaded => Crash
            checkConnections();
            updateDone =true;
        }
    }

    public String getModelPath() {
        return RfAdditions.MOD_ID+":rfcable";
    }

    public Material getMaterial() {
        return Material.iron;
    }

    public float getHardness(PartMOP hit) {
        return 3.0F;
    }

    public BlockState createBlockState() {
        return new ExtendedBlockState(MCMultiPartMod.multipart, new IProperty[]{cableType} , sides);
    }

    @Override
    public int receiveEnergy(EnumFacing facing, int maxReceive, boolean simulate) {
        return 0;
    }

    @Override
    public int extractEnergy(EnumFacing facing, int maxExtract, boolean simulate) {
        return 0;
    }

    @Override
    public int getEnergyStored(EnumFacing facing) {
        return 0;
    }

    @Override
    public int getMaxEnergyStored(EnumFacing facing) {
        return 0;
    }

    @Override
    public boolean canConnectEnergy(EnumFacing facing) {
        return false;
    }

    private boolean canConnect(TileEntity handler, EnumFacing face){
        if(handler == null) return false;
        return true;
        /*else if(handler instanceof IMultipartContainer){
            IMultipartContainer container = (IMultipartContainer) handler;
            Collection<? extends IMultipart> parts = container.getParts();
            for (IMultipart p : parts){
                if(p instanceof IEnergyConnection)
                    return ((IEnergyConnection)p).canConnectEnergy(face.getOpposite());
            }
        }
        else if(handler instanceof IEnergyConnection){
            return ((IEnergyConnection)handler).canConnectEnergy(face.getOpposite());
        }*/
        //return false;
    }

    public IBlockState getExtendedState(IBlockState state) {
        for(EnumFacing f : EnumFacing.VALUES){
            if(this.getContainer() == null || this.getPos()==null || this.getWorld()==null || sides[f.ordinal()]==null) continue;
            BlockPos tilepos = getPos().add(f.getFrontOffsetX(), f.getFrontOffsetY(), f.getFrontOffsetY());
            ((IExtendedBlockState)state).withProperty(sides[f.ordinal()], true);
        }
        if(type!=null){
            state.withProperty(cableType,type);
        }
        else
        {
            state.withProperty(cableType,EnumCableType.CONDUCTIVE_REDSTONE);
        }

        Collection<IUnlistedProperty<?>> test = ((IExtendedBlockState) state).getUnlistedNames();
        for(IUnlistedProperty<?> t : test){
             System.out.println(t.getName()+ " : "+((IExtendedBlockState)state).getValue(t)); // getValue is null
        }

        return state;
    }

    private void checkConnections(){
        connections.clear();
        IMultipartContainer container = this.getContainer();
        for(EnumFacing facing : EnumFacing.VALUES) {
            BlockPos changed = container.getPosIn().add(facing.getFrontOffsetX(), facing.getFrontOffsetY(), facing.getFrontOffsetY());
            connections.put(facing, canConnect(container.getWorldIn().getTileEntity(changed), facing));
        }
    }

    public void onNeighborBlockChange(Block block) {
        super.onNeighborBlockChange(block);
        checkConnections();
        if(this.getContainer() != null && this.getPos()!=null){
            getWorld().markBlockForUpdate(getPos());
        }
    }

    public void onAdded() {
        super.onAdded();
        checkConnections();
    }

    @Override
    public EnumSet<PartSlot> getSlotMask() {
        return EnumSet.of(PartSlot.CENTER);
    }
}

 

Property Class

public class PropertyCableConnection{

        public static IUnlistedProperty<Boolean>[] getPropertyList(){
            IUnlistedProperty[] p = new IUnlistedProperty[EnumFacing.VALUES.length];
            int i=0;
            for(EnumFacing f: EnumFacing.VALUES){
                p[i]= Properties.toUnlisted(PropertyBool.create(f.getName()));
                i++;
            }
            return p;
        }

        public static boolean getSide(EnumFacing face, IBlockState state){
            if(!(state instanceof IExtendedBlockState)) return false;
            Collection<IUnlistedProperty<?>> props = ((IExtendedBlockState)state).getUnlistedNames();
            IUnlistedProperty<?> found = null;
            for (IUnlistedProperty<?> p : props){
                if(p.getName() == face.getName() && p.getType()==Boolean.class)
                    found=p;
            }
            if(found==null) return false;
            return ((IExtendedBlockState)state).getValue((IUnlistedProperty<Boolean>) found); // NullPointerException
        }
}

Link to comment
Share on other sites

I found the problem it seems like withProperty() for the IUnlistedProperty doesn't change the state, it just returns the new state.

 

For everyone who has the same problem, here is the new method:

public IBlockState getExtendedState(IBlockState state) {
     checkConnections();
     return ((IExtendedBlockState) state).withProperty(PROPERTY_CONNECTION, this.connections).withProperty(PROPERTY_TYPE, this.type);
}

 

I also use a unlisted Byte property, where the first 6 bits are the 6 sides of the block.

Link to comment
Share on other sites

I found the problem it seems like withProperty() for the IUnlistedProperty doesn't change the state, it just returns the new state.

 

This is true of the vanilla

IBlockState

implementation as well, it's immutable.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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

    • Whether you are a fan of Hypixel Bedwars, SkyWars and PvP gamemodes like that, well you would enjoy this server! We have a very fun and unique style of PvP that a lot of our players really enjoy and we want to bring this server to more players like you! Yes you reading this post haha. Introducing, the Minezone Network, home of SUPER CRAFT BLOCKS. We've been working on this server for over 4 years now. Here is what we have to offer: SUPER CRAFT BLOCKS: This has 3 different gamemodes you can play, Classic, Duels and Frenzy. Each mode offers over 60 kits to choose from, along with a total of over 60 maps, allowing for various different playstyles on each map. There are also random powerups that spawn on the map which can include Health Pots, Bazookas, Nukes, Extra Lives and way way more! There is also double jump in this gamemode as well, which makes PvP a lot more fun & unique. You only need a minimum of 2 players to start any mode! Classic: Choose a kit, 5 lives for each player, fight it out and claim the #1 spot! Look out for lightning as they can spawn powerups to really give you an advantage in the game! Duels: Fight against another random player or one of your friends and see who is the best! Frenzy: Your kit is randomly selected for you, each life you will have a different kit. You can fight with up to 100 players in this mode and lets see who will be the best out of that 100! All the other stuff from Classic/Duels apply to this mode as well like powerups. We have 2 ranks on this server too, VIP and CAPTAIN which has a bunch of different perks for SCB and other things like Cosmetics and more.   SERVER IP: If this server has caught your interest in any way, please consider joining and you will NOT regret it! Bring some of your friends online for an even better experience and join in on the fun at: IP: minezone.club Hope to see you online!   SERVER TRAILER: https://www.youtube.com/watch?v=0phpMgu1mH0
    • The mod give new blocks  
    • I will a Mode for 1.21 in this Mod give new block, items and dimensions   
    • Remove Optifine For shader support and similar, use Embeddium + Oculus
  • Topics

×
×
  • Create New...

Important Information

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