Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mfnalex committed Dec 22, 2021
0 parents commit a30bcf3
Show file tree
Hide file tree
Showing 7 changed files with 340 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Project exclude paths
/target/
66 changes: 66 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.jeff_media</groupId>
<artifactId>CustomBlocks</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>jitpack-repo</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.18.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.mojang</groupId>-->
<!-- <artifactId>authlib</artifactId>-->
<!-- <version>3.2.38</version>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>de.jeff_media</groupId>
<artifactId>JeffLib</artifactId>
<version>7.1.10-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.LoneDev6</groupId>
<artifactId>api-itemsadder</artifactId>
<version>2.5.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.th0rgal</groupId>
<artifactId>oraxen-mfnalex</artifactId>
<version>1.121.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
62 changes: 62 additions & 0 deletions src/main/java/de/jeff_media/customblocks/CustomBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package de.jeff_media.customblocks;

import de.jeff_media.customblocks.implentation.HeadBlock;
import de.jeff_media.customblocks.implentation.ItemsAdderBlock;
import de.jeff_media.customblocks.implentation.OraxenBlock;
import de.jeff_media.customblocks.implentation.VanillaBlock;
import de.jeff_media.jefflib.exceptions.InvalidBlockDataException;
import de.jeff_media.jefflib.exceptions.MissingPluginException;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.plugin.Plugin;

import java.util.Locale;

public abstract class CustomBlock {

public static CustomBlock fromString(String fullId) throws InvalidBlockDataException, MissingPluginException {
if (fullId.startsWith("minecraft:") || !fullId.contains(":")) {
return new VanillaBlock(fullId);
}

String[] split = fullId.split(":");
if(split.length==1) {
throw new InvalidBlockDataException("Could not parse custom block data: " + fullId);
}

String namespace = split[0];
String id = split[1];

switch (namespace.toLowerCase(Locale.ROOT)) {
case "head":
return new HeadBlock(id);
case "itemsadder":
checkForPlugin("itemsadder","ItemsAdder");
return new ItemsAdderBlock(id);
case "oraxen":
checkForPlugin("oraxen","Oraxen");
return new OraxenBlock(id);
}

throw new InvalidBlockDataException("Could not parse custom block data: " + fullId);
}

private static void checkForPlugin(String namespace, String pluginName) throws MissingPluginException {
Plugin plugin = Bukkit.getPluginManager().getPlugin(pluginName);
if(plugin == null || !plugin.isEnabled()) {
throw new MissingPluginException(String.format("Placing custom blocks from namespace \"%s\" requires the following plugin to be installed: \"%s\"",
namespace, pluginName));
}
}

public abstract void place(Block block);

public CustomBlock(String id) {
this.id = id;
};

public abstract String getNamespace();

@Getter private final String id;
}
109 changes: 109 additions & 0 deletions src/main/java/de/jeff_media/customblocks/implentation/HeadBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package de.jeff_media.customblocks.implentation;

import de.jeff_media.customblocks.CustomBlock;
import de.jeff_media.jefflib.SkullUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Skull;
import org.bukkit.entity.Player;

import java.util.UUID;

public class HeadBlock extends CustomBlock {

public HeadBlock(String id) {
super(id);
}

@Override
public void place(Block block) {
place(block,null);
}

public void place(Block block, OfflinePlayer player) {
block.setType(Material.AIR);
block.setType(Material.PLAYER_HEAD);
System.out.println("Placing HeadBlock");
// Set dynamic player
if(getId().equalsIgnoreCase("player")) {
System.out.println("Using dynamic player");
if(player == null) {
throw new IllegalArgumentException("Using head:player requires an OfflinePlayer");
}
setOfflinePlayer(block, player);
return;
}

// Set static player from name
if(isValidAccountName(getId())) {
System.out.println("Using static player name");
setOfflinePlayer(block, getOfflinePlayerByName(getId()));
return;
}

// Set static player from UUID
if(isValidUUID(getId())) {
System.out.println("Using static player UUID");
setOfflinePlayer(block, getOfflinePlayerByUUID(getId()));
return;
}

// Base64 Texture
System.out.println("Using Base64: " + getId());
setBase64Texture(block);
}

private void setBase64Texture(Block block) {
BlockState state = block.getState();
if(state instanceof Skull) {
/*final Skull skull = (Skull) state;
final GameProfile profile = new GameProfile(UUID.randomUUID(),"sk");
profile.getProperties().put("textures", new Property("textures",getId()));
*/
SkullUtils.setBase64Texture(block, getId());
}
}

private static void setOfflinePlayer(Block block, OfflinePlayer player) {
BlockState state = block.getState();
if(state instanceof Skull) {
final Skull skull = (Skull) state;
skull.setOwningPlayer(player);
skull.update();
}
}

private static boolean isValidAccountName(String name) {
return name.matches("^\\w{3,16}$");
}

private static OfflinePlayer getOfflinePlayerByName(String name) {
final Player player = Bukkit.getPlayerExact(name);
if(player != null) return player;
return Bukkit.getOfflinePlayer(name);
}

private OfflinePlayer getOfflinePlayerByUUID(String string) {
if(string.length()==36) return Bukkit.getOfflinePlayer(UUID.fromString(string));
if(string.length()==32) return Bukkit.getOfflinePlayer(fromStringWithoutDashes(string));
throw new IllegalArgumentException("Not a valid UUID: " + getId());
}

private static UUID fromStringWithoutDashes(String string) {
return UUID.fromString(string
.replaceFirst("(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)", "$1-$2-$3-$4-$5"));
}

private static boolean isValidUUID(String string) {
return string.replace("-","").matches("^\\p{XDigit}{32}$");
}


@Override
public String getNamespace() {
return "head";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package de.jeff_media.customblocks.implentation;

import de.jeff_media.customblocks.CustomBlock;
import de.jeff_media.jefflib.exceptions.InvalidBlockDataException;
import org.bukkit.block.Block;

public class ItemsAdderBlock extends CustomBlock {

dev.lone.itemsadder.api.CustomBlock iaBlock;

public ItemsAdderBlock(String id) throws InvalidBlockDataException {
super(id);
iaBlock = dev.lone.itemsadder.api.CustomBlock.getInstance(id);
if(iaBlock == null) throw new InvalidBlockDataException("Could not find ItemsAdder block: " + id);
}

@Override
public void place(Block block) {
iaBlock.place(block.getLocation());
}

@Override
public String getNamespace() {
return "itemsadder";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.jeff_media.customblocks.implentation;

import de.jeff_media.customblocks.CustomBlock;
import de.jeff_media.jefflib.exceptions.InvalidBlockDataException;
import io.th0rgal.oraxen.mechanics.provided.gameplay.noteblock.NoteBlockMechanicFactory;
import org.bukkit.block.Block;

public class OraxenBlock extends CustomBlock {

public OraxenBlock(String id) throws InvalidBlockDataException {
super(id);
if(NoteBlockMechanicFactory.getInstance().getMechanic(id) == null) throw new InvalidBlockDataException("Could not find Oraxen block: " + id);
}

@Override
public void place(Block block) {
NoteBlockMechanicFactory.setBlockModel(block, getId());
}

@Override
public String getNamespace() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package de.jeff_media.customblocks.implentation;

import com.google.common.base.Enums;
import de.jeff_media.customblocks.CustomBlock;
import de.jeff_media.jefflib.exceptions.InvalidBlockDataException;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;

import java.util.Locale;

public class VanillaBlock extends CustomBlock {

@Getter
@Setter
private final BlockData blockData;

public VanillaBlock(String id) throws InvalidBlockDataException {
super(id.replace("minecraft:",""));

// Vanilla Material
if (!id.contains(":") && !id.contains("[")) {
final Material material = Enums.getIfPresent(Material.class, id.toUpperCase(Locale.ROOT)).orNull();
if (material == null) throw new InvalidBlockDataException("Could not find material with ID " + id);
this.blockData = Bukkit.createBlockData(material);
return;
}

// Vanilla BlockData
try {
this.blockData = Bukkit.createBlockData(id.startsWith("minecraft:") ? id : "minecraft:" + id);
} catch (IllegalArgumentException exception) {
throw new InvalidBlockDataException("Could not parse blockdata: " + id);
}

}

@Override
public String getNamespace() {
return "minecraft";
}


@Override
public void place(Block block) {
block.setBlockData(blockData);
}
}

0 comments on commit a30bcf3

Please sign in to comment.