All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.nukkit.blockstate.ZeroMutableBlockState Maven / Gradle / Ivy

There is a newer version: 1.20.40-r1
Show newest version
/*
 * https://PowerNukkit.org - The Nukkit you know but Powerful!
 * Copyright (C) 2020  José Roberto de Araújo Júnior
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package cn.nukkit.blockstate;

import cn.nukkit.api.PowerNukkitOnly;
import cn.nukkit.api.Since;
import cn.nukkit.api.Unsigned;
import cn.nukkit.blockproperty.BlockProperties;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.NoSuchElementException;

import static cn.nukkit.blockstate.IMutableBlockState.handleUnsupportedStorageType;

/**
 * @author joserobjr
 * @since 2020-10-03
 */
@PowerNukkitOnly
@Since("1.4.0.0-PN")
@ParametersAreNonnullByDefault
public class ZeroMutableBlockState extends MutableBlockState {
    private final BlockState state;

    @PowerNukkitOnly
    @Since("1.4.0.0-PN")
    public ZeroMutableBlockState(int blockId, BlockProperties properties) {
        super(blockId, properties);
        state = BlockState.of(blockId);
    }

    @PowerNukkitOnly
    @Override
    public void validate() {
    }

    @PowerNukkitOnly
    @Nonnull
    @Override
    public ZeroMutableBlockState copy() {
        return this;
    }

    @Since("1.4.0.0-PN")
    @PowerNukkitOnly
    @Override
    public void setDataStorage(@Nonnegative Number storage) {
        Class c = storage.getClass();
        int state;
        if (c == Integer.class || c == Short.class || c == Byte.class) {
            state = storage.intValue();
        } else {
            try {
                state = new BigDecimal(storage.toString()).intValueExact();
            } catch (ArithmeticException | NumberFormatException e) {
                throw handleUnsupportedStorageType(getBlockId(), storage, e);
            }
        }
        if (state != 0) {
            throw handleUnsupportedStorageType(getBlockId(), storage, new ArithmeticException("ZeroMutableBlockState only accepts zero"));
        }
    }

    @Since("1.4.0.0-PN")
    @PowerNukkitOnly
    @Override
    public void setDataStorageFromInt(@Nonnegative int storage) {
        if (storage != 0) {
            throw handleUnsupportedStorageType(getBlockId(), storage, new ArithmeticException("ZeroMutableBlockState only accepts zero"));
        }
    }

    @Since("1.4.0.0-PN")
    @PowerNukkitOnly
    @Override
    public void setPropertyValue(String propertyName, @Nullable Serializable value) {
        throw new NoSuchElementException("ZeroMutableBlockState can't have properties. Attempted to set "+propertyName+" to "+value);
    }

    @Since("1.4.0.0-PN")
    @PowerNukkitOnly
    @Override
    public void setBooleanValue(String propertyName, boolean value) {
        throw new NoSuchElementException("ZeroMutableBlockState can't have properties. Attempted to set "+propertyName+" to "+value);
    }

    @Since("1.4.0.0-PN")
    @PowerNukkitOnly
    @Override
    public void setIntValue(String propertyName, int value) {
        throw new NoSuchElementException("ZeroMutableBlockState can't have properties. Attempted to set "+propertyName+" to "+value);
    }

    @Nonnegative
    @Since("1.4.0.0-PN")
    @PowerNukkitOnly
    @Nonnull
    @Override
    public Number getDataStorage() {
        return 0;
    }

    @Since("1.4.0.0-PN")
    @PowerNukkitOnly
    @Override
    public boolean isDefaultState() {
        return true;
    }

    @Nonnegative
    @Since("1.4.0.0-PN")
    @PowerNukkitOnly
    @Override
    public int getLegacyDamage() {
        return 0;
    }

    @Unsigned
    @Since("1.4.0.0-PN")
    @Override
    @PowerNukkitOnly
    public int getBigDamage() {
        return 0;
    }

    @Nonnegative
    @Since("1.4.0.0-PN")
    @PowerNukkitOnly
    @Nonnull
    @Override
    public BigInteger getHugeDamage() {
        return BigInteger.ZERO;
    }

    @Since("1.4.0.0-PN")
    @PowerNukkitOnly
    @Nonnull
    @Override
    public Serializable getPropertyValue(String propertyName) {
        throw new NoSuchElementException("ZeroMutableBlockState can't have properties. Attempted get property "+propertyName);
    }

    @Since("1.4.0.0-PN")
    @PowerNukkitOnly
    @Override
    public int getIntValue(String propertyName) {
        throw new NoSuchElementException("ZeroMutableBlockState can't have properties. Attempted get property "+propertyName);
    }

    @Since("1.4.0.0-PN")
    @PowerNukkitOnly
    @Override
    public boolean getBooleanValue(String propertyName) {
        throw new NoSuchElementException("ZeroMutableBlockState can't have properties. Attempted get property "+propertyName);
    }

    @Since("1.4.0.0-PN")
    @PowerNukkitOnly
    @Nonnull
    @Override
    public String getPersistenceValue(String propertyName) {
        throw new NoSuchElementException("ZeroMutableBlockState can't have properties. Attempted get property "+propertyName);
    }

    @Since("1.4.0.0-PN")
    @PowerNukkitOnly
    @Nonnull
    @Override
    public BlockState getCurrentState() {
        return state;
    }

    @Since("1.4.0.0-PN")
    @PowerNukkitOnly
    @Override
    public int getExactIntStorage() {
        return 0;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy