-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
140 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/main/java/de/jeff_media/customblocks/implentation/NexoBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package de.jeff_media.customblocks.implentation; | ||
|
||
import com.jeff_media.jefflib.exceptions.InvalidBlockDataException; | ||
import com.nexomc.nexo.api.NexoBlocks; | ||
import com.nexomc.nexo.api.NexoFurniture; | ||
import com.nexomc.nexo.mechanics.custom_block.noteblock.NoteBlockMechanicFactory; | ||
import com.nexomc.nexo.mechanics.furniture.FurnitureMechanic; | ||
import de.jeff_media.customblocks.CustomBlock; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.Material; | ||
import org.bukkit.OfflinePlayer; | ||
import org.bukkit.block.Block; | ||
import org.bukkit.block.BlockFace; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.entity.ItemDisplay; | ||
|
||
import java.util.Objects; | ||
|
||
public class NexoBlock extends CustomBlock { | ||
|
||
private FurnitureMechanic furnitureMechanic = null; | ||
private ItemType type; | ||
|
||
public NexoBlock(String id) throws InvalidBlockDataException { | ||
super(id); | ||
|
||
// Blocks | ||
if (NexoBlocks.noteBlockMechanic(id) != null) { | ||
type = ItemType.NOTE_BLOCK; | ||
return; | ||
} | ||
|
||
// Furniture | ||
FurnitureMechanic mechanicFactory = NexoFurniture.furnitureMechanic(id); | ||
if (mechanicFactory != null) { | ||
type = ItemType.FURNITURE; | ||
furnitureMechanic = mechanicFactory; | ||
return; | ||
} | ||
|
||
|
||
throw new InvalidBlockDataException("Could not find Nexo block: " + id); | ||
} | ||
|
||
private static float getYaw() { | ||
return Float.parseFloat(System.getProperty("customblocks.nexo.yaw", "0")); | ||
} | ||
|
||
private static BlockFace getBlockFace() { | ||
return BlockFace.valueOf(System.getProperty("customblocks.nexo.blockface", "DOWN")); | ||
} | ||
|
||
|
||
@Override | ||
public void place(Block block, OfflinePlayer player) { | ||
super.place(block, player); | ||
switch (type) { | ||
case FURNITURE: | ||
block.setType(Material.AIR); | ||
Entity placed = furnitureMechanic.place(block.getLocation(), getYaw(), getBlockFace()); | ||
if (placed != null) { | ||
entities.add(placed.getUniqueId()); | ||
} | ||
break; | ||
case NOTE_BLOCK: | ||
NoteBlockMechanicFactory.Companion.setBlockModel(block, getId()); | ||
break; | ||
|
||
default: | ||
throw new IllegalStateException(); | ||
} | ||
} | ||
|
||
@Override | ||
public String getNamespace() { | ||
return "nexo"; | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
if (furnitureMechanic != null) { | ||
|
||
entities.stream().map(Bukkit::getEntity).filter(Objects::nonNull).forEach(entity -> { | ||
try { | ||
furnitureMechanic.removeBaseEntity((ItemDisplay) entity); | ||
} catch (Exception ignored) { | ||
} | ||
}); | ||
|
||
} | ||
super.remove(); | ||
} | ||
|
||
@Override | ||
public Material getMaterial() { | ||
switch (type) { | ||
case FURNITURE: | ||
return Material.BARRIER; | ||
case NOTE_BLOCK: | ||
return Material.NOTE_BLOCK; | ||
default: | ||
throw new IllegalStateException(); | ||
} | ||
} | ||
|
||
private enum ItemType { | ||
NOTE_BLOCK, FURNITURE | ||
} | ||
} |