Skip to content

Commit

Permalink
feat: improved copper bulb (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
KoshakMineDEV authored Oct 23, 2024
1 parent 1eda417 commit 986aa41
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 20 deletions.
7 changes: 7 additions & 0 deletions src/main/java/cn/nukkit/block/BlockCopperBulb.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cn.nukkit.block;


import cn.nukkit.block.properties.enums.OxidizationLevel;
import cn.nukkit.item.Item;
import cn.nukkit.item.ItemBlock;
import cn.nukkit.utils.BlockColor;
import org.jetbrains.annotations.NotNull;

public class BlockCopperBulb extends BlockCopperBulbBase {

Expand Down Expand Up @@ -41,4 +43,9 @@ public BlockColor getColor() {
public int getLightLevel() {
return this.isLit() ? 15 : 0;
}

@Override
public @NotNull OxidizationLevel getOxidizationLevel() {
return OxidizationLevel.UNAFFECTED;
}
}
103 changes: 83 additions & 20 deletions src/main/java/cn/nukkit/block/BlockCopperBulbBase.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package cn.nukkit.block;

import cn.nukkit.Player;
import cn.nukkit.block.properties.enums.OxidizationLevel;
import cn.nukkit.event.redstone.RedstoneUpdateEvent;
import cn.nukkit.item.Item;
import cn.nukkit.level.Level;
import cn.nukkit.math.BlockFace;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* @author glorydark
*/
public abstract class BlockCopperBulbBase extends BlockSolidMeta {
public abstract class BlockCopperBulbBase extends BlockSolidMeta implements Oxidizable, Waxable {

public static final int LIT_BIT = 0x01; // 0001

Expand All @@ -23,39 +26,45 @@ public BlockCopperBulbBase(int meta) {
super(meta);
}

@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
if (this.level.isBlockPowered(this.getLocation())) {
this.setLit(true);
this.setPowered(true);
}
return this.getLevel().setBlock(this, this, true, true);
}

@Override
public int onUpdate(int type) {
if (type == Level.BLOCK_UPDATE_NORMAL || type == Level.BLOCK_UPDATE_REDSTONE) {
// Redstone event
Oxidizable.super.onUpdate(type);

if (type == Level.BLOCK_UPDATE_REDSTONE) {
RedstoneUpdateEvent ev = new RedstoneUpdateEvent(this);
getLevel().getServer().getPluginManager().callEvent(ev);

if (ev.isCancelled()) {
return 0;
}
if (this.level.isBlockPowered(this.getLocation())) {
this.setLit(true);

if ((this.level.isBlockPowered(this))) {
this.setLit(!(isLit()));
this.setPowered(true);
this.level.setBlock(this, this, true);
return type;
} else if (this.isLit() || this.isPowered()) {
this.setLit(false);
this.getLevel().setBlock(this, this, true, true);
return 1;
}

if(isPowered()) {
this.setPowered(false);
this.level.setBlock(this, this, true);
return type;
this.getLevel().setBlock(this, this, true, true);
return 1;
}
}
return 0;
}

@Override
public boolean onActivate(@NotNull Item item, Player player) {
return Waxable.super.onActivate(item, player)
|| Oxidizable.super.onActivate(item, player);
}

@Override
public boolean canBeActivated() {
return true;
}

@Override
public double getHardness() {
return 0.3D;
Expand All @@ -81,4 +90,58 @@ public boolean isPowered() {
public void setPowered(boolean lit) {
this.setDamage(POWERED_BIT, lit ? 1 : 0);
}

@Override
public boolean isWaxed() {
return false;
}

@Override
public Block getStateWithOxidizationLevel(@NotNull OxidizationLevel oxidizationLevel) {
BlockCopperBulbBase bulb = (BlockCopperBulbBase) Block.get((getCopperId(isWaxed(), oxidizationLevel)));
bulb.setDamage(getDamage());
return bulb;
}

@Override
public boolean setOxidizationLevel(@NotNull OxidizationLevel oxidizationLevel) {
if (getOxidizationLevel().equals(oxidizationLevel)) {
return true;
}

BlockCopperBulbBase bulb = (BlockCopperBulbBase) Block.get((getCopperId(isWaxed(), oxidizationLevel)));
bulb.setDamage(getDamage());

return getValidLevel().setBlock(this, bulb);
}

@Override
public boolean setWaxed(boolean waxed) {
if (isWaxed() == waxed) {
return true;
}

BlockCopperBulbBase bulb = (BlockCopperBulbBase) Block.get((getCopperId(waxed, getOxidizationLevel())));
bulb.setDamage(getDamage());

return getValidLevel().setBlock(this, bulb);
}

protected int getCopperId(boolean waxed, @Nullable OxidizationLevel oxidizationLevel) {
if (oxidizationLevel == null) {
return getId();
}
switch (oxidizationLevel) {
case UNAFFECTED:
return waxed? WAXED_COPPER_BULB : COPPER_BULB;
case EXPOSED:
return waxed? WAXED_EXPOSED_COPPER_BULB : EXPOSED_COPPER_BULB;
case WEATHERED:
return waxed? WAXED_WEATHERED_COPPER_BULB : WEATHERED_COPPER_BULB;
case OXIDIZED:
return waxed? WAXED_OXIDIZED_COPPER_BULB : OXIDIZED_COPPER_BULB;
default:
return getId();
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/cn/nukkit/block/BlockExposedCopperBulb.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cn.nukkit.block;

import cn.nukkit.block.properties.enums.OxidizationLevel;
import cn.nukkit.utils.BlockColor;
import org.jetbrains.annotations.NotNull;

public class BlockExposedCopperBulb extends BlockCopperBulbBase {

Expand Down Expand Up @@ -31,4 +33,9 @@ public BlockColor getColor() {
public int getLightLevel() {
return this.isLit() ? 12 : 0;
}

@Override
public @NotNull OxidizationLevel getOxidizationLevel() {
return OxidizationLevel.EXPOSED;
}
}
7 changes: 7 additions & 0 deletions src/main/java/cn/nukkit/block/BlockOxidizedCopperBulb.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cn.nukkit.block;

import cn.nukkit.block.properties.enums.OxidizationLevel;
import cn.nukkit.utils.BlockColor;
import org.jetbrains.annotations.NotNull;

public class BlockOxidizedCopperBulb extends BlockCopperBulbBase {

Expand Down Expand Up @@ -31,4 +33,9 @@ public BlockColor getColor() {
public int getLightLevel() {
return this.isLit() ? 4 : 0;
}

@Override
public @NotNull OxidizationLevel getOxidizationLevel() {
return OxidizationLevel.OXIDIZED;
}
}
12 changes: 12 additions & 0 deletions src/main/java/cn/nukkit/block/BlockWaxedCopperBulb.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cn.nukkit.block;

import cn.nukkit.block.properties.enums.OxidizationLevel;
import cn.nukkit.utils.BlockColor;
import org.jetbrains.annotations.NotNull;

public class BlockWaxedCopperBulb extends BlockCopperBulbBase {

Expand Down Expand Up @@ -31,4 +33,14 @@ public BlockColor getColor() {
public int getLightLevel() {
return this.isLit() ? 15 : 0;
}

@Override
public @NotNull OxidizationLevel getOxidizationLevel() {
return OxidizationLevel.UNAFFECTED;
}

@Override
public boolean isWaxed() {
return true;
}
}
12 changes: 12 additions & 0 deletions src/main/java/cn/nukkit/block/BlockWaxedExposedCopperBulb.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cn.nukkit.block;

import cn.nukkit.block.properties.enums.OxidizationLevel;
import cn.nukkit.utils.BlockColor;
import org.jetbrains.annotations.NotNull;

public class BlockWaxedExposedCopperBulb extends BlockCopperBulbBase {

Expand Down Expand Up @@ -31,4 +33,14 @@ public BlockColor getColor() {
public int getLightLevel() {
return this.isLit() ? 12 : 0;
}

@Override
public @NotNull OxidizationLevel getOxidizationLevel() {
return OxidizationLevel.EXPOSED;
}

@Override
public boolean isWaxed() {
return true;
}
}
12 changes: 12 additions & 0 deletions src/main/java/cn/nukkit/block/BlockWaxedOxidizedCopperBulb.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cn.nukkit.block;

import cn.nukkit.block.properties.enums.OxidizationLevel;
import cn.nukkit.utils.BlockColor;
import org.jetbrains.annotations.NotNull;

public class BlockWaxedOxidizedCopperBulb extends BlockCopperBulbBase {

Expand Down Expand Up @@ -31,4 +33,14 @@ public BlockColor getColor() {
public int getLightLevel() {
return this.isLit() ? 4 : 0;
}

@Override
public @NotNull OxidizationLevel getOxidizationLevel() {
return OxidizationLevel.OXIDIZED;
}

@Override
public boolean isWaxed() {
return true;
}
}
12 changes: 12 additions & 0 deletions src/main/java/cn/nukkit/block/BlockWaxedWeatheredCopperBulb.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cn.nukkit.block;

import cn.nukkit.block.properties.enums.OxidizationLevel;
import cn.nukkit.utils.BlockColor;
import org.jetbrains.annotations.NotNull;

public class BlockWaxedWeatheredCopperBulb extends BlockCopperBulbBase {

Expand Down Expand Up @@ -31,4 +33,14 @@ public BlockColor getColor() {
public int getLightLevel() {
return this.isLit() ? 8 : 0;
}

@Override
public @NotNull OxidizationLevel getOxidizationLevel() {
return OxidizationLevel.WEATHERED;
}

@Override
public boolean isWaxed() {
return true;
}
}
7 changes: 7 additions & 0 deletions src/main/java/cn/nukkit/block/BlockWeatheredCopperBulb.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cn.nukkit.block;

import cn.nukkit.block.properties.enums.OxidizationLevel;
import cn.nukkit.utils.BlockColor;
import org.jetbrains.annotations.NotNull;

public class BlockWeatheredCopperBulb extends BlockCopperBulbBase {

Expand Down Expand Up @@ -31,4 +33,9 @@ public BlockColor getColor() {
public int getLightLevel() {
return this.isLit() ? 8 : 0;
}

@Override
public @NotNull OxidizationLevel getOxidizationLevel() {
return OxidizationLevel.WEATHERED;
}
}

0 comments on commit 986aa41

Please sign in to comment.