Skip to content

Commit

Permalink
- Finished javadocs, annotations and null checkers in sign system.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hakan Kargın committed Mar 18, 2022
1 parent cf5d11c commit cdcea25
Show file tree
Hide file tree
Showing 20 changed files with 311 additions and 72 deletions.
2 changes: 1 addition & 1 deletion api/src/main/java/com/hakan/core/HCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public static HSign getSignByUID(UUID uid) {
return HSignHandler.getByUID(uid);
}

public static HSign createSign(Material type, String... lines) {
public static HSign createSign(@Nonnull Material type, @Nonnull String... lines) {
return HSignHandler.create(type, lines);
}

Expand Down
79 changes: 68 additions & 11 deletions api/src/main/java/com/hakan/core/ui/sign/HSign.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,100 @@
import org.bukkit.Material;
import org.bukkit.entity.Player;

import javax.annotation.Nonnull;
import java.util.Objects;
import java.util.function.Consumer;

/**
* HSign class to manage
* and show sign to player.
*/
public abstract class HSign {

protected Material type;
protected String[] lines;
protected Consumer<String[]> consumer;

public HSign(Material type, String... lines) {
this.type = type;
this.lines = lines;
/**
* Creates new instance of this class.
*
* @param type Type of sign.
* @param lines Lines of sign.
*/
public HSign(@Nonnull Material type, @Nonnull String... lines) {
this.type = Objects.requireNonNull(type, "type cannot be null!");
this.lines = Objects.requireNonNull(lines, "lines cannot be null!");
}

/**
* Gets type of sign.
*
* @return Type of sign.
*/
@Nonnull
public final Material getType() {
return this.type;
}

public final HSign setType(Material type) {
this.type = type;
/**
* Sets type of sign.
*
* @param type Type of sign.
* @return This class.
*/
@Nonnull
public final HSign setType(@Nonnull Material type) {
this.type = Objects.requireNonNull(type, "type cannot be null!");
return this;
}

/**
* Gets line of sign.
*
* @return Lines of sign.
*/
@Nonnull
public final String[] getLines() {
return this.lines;
}

public final HSign setLines(String[] lines) {
this.lines = lines;
/**
* Sets lines of sign.
*
* @param lines Lines of sign.
* @return This class.
*/
@Nonnull
public final HSign setLines(@Nonnull String[] lines) {
this.lines = Objects.requireNonNull(lines, "lines cannot be null!");
return this;
}

public final HSign onComplete(Consumer<String[]> consumer) {
this.consumer = consumer;
/**
* Runs when sign close.
*
* @param consumer Callback.
* @return This class.
*/
@Nonnull
public final HSign onComplete(@Nonnull Consumer<String[]> consumer) {
this.consumer = Objects.requireNonNull(consumer, "complete consumer cannot be null!");
return this;
}

public abstract void open(Player player);
/**
* Opens the sign to player.
*
* @param player Player.
*/
public abstract void open(@Nonnull Player player);

public abstract <T> void listen(Player player, T packet);
/**
* Listens player packet if
* packet is a sign packet.
*
* @param player Player.
* @param packet Packet.
*/
public abstract <T> void listen(@Nonnull Player player, @Nonnull T packet);
}
93 changes: 83 additions & 10 deletions api/src/main/java/com/hakan/core/ui/sign/HSignHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,132 @@
import com.hakan.core.HCore;
import com.hakan.core.listener.HListenerAdapter;
import com.hakan.core.ui.sign.listeners.HSignListener;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import javax.annotation.Nonnull;
import java.util.*;

public class HSignHandler {
/**
* HSignHandler class to find
* and create the signs.
*/
public final class HSignHandler {

private static final Map<UUID, HSign> signMap = new HashMap<>();

public static void initialize(JavaPlugin plugin) {
/**
* Initializes sign system.
*
* @param plugin Main class of plugin.
*/
public static void initialize(@Nonnull JavaPlugin plugin) {
HListenerAdapter.register(new HSignListener(plugin));
}


/**
* Gets content as safe.
*
* @return Content.
*/
@Nonnull
public static Map<UUID, HSign> getContentSafe() {
return new HashMap<>(HSignHandler.signMap);
}

/**
* Gets content.
*
* @return Content
*/
@Nonnull
public static Map<UUID, HSign> getContent() {
return HSignHandler.signMap;
}

/**
* Gets values as safe.
*
* @return Values.
*/
@Nonnull
public static Collection<HSign> getValuesSafe() {
return new ArrayList<>(HSignHandler.signMap.values());
}

/**
* Gets values.
*
* @return Values.
*/
@Nonnull
public static Collection<HSign> getValues() {
return HSignHandler.signMap.values();
}

public static Optional<HSign> findByPlayer(Player player) {
/**
* Finds HSign by player.
*
* @param player Player.
* @return HSign as optional.
*/
@Nonnull
public static Optional<HSign> findByPlayer(@Nonnull Player player) {
return HSignHandler.findByUID(player.getUniqueId());
}

public static HSign getByPlayer(Player player) {
/**
* Gets HSign by player.
*
* @param player Player.
* @return HSign.
*/
@Nonnull
public static HSign getByPlayer(@Nonnull Player player) {
return HSignHandler.findByPlayer(player).orElseThrow(() -> new NullPointerException("this player doesn't have a inventory!"));
}

public static Optional<HSign> findByUID(UUID uid) {
return Optional.ofNullable(HSignHandler.signMap.get(uid));
/**
* Finds HSign by player UID.
*
* @param uid Player UID.
* @return HSign as optional.
*/
@Nonnull
public static Optional<HSign> findByUID(@Nonnull UUID uid) {
return Optional.ofNullable(HSignHandler.signMap.get(Objects.requireNonNull(uid, "UID cannot be null!")));
}

public static HSign getByUID(UUID uid) {
/**
* Gets HSign by player UID.
*
* @param uid Player UID.
* @return HSign.
*/
@Nonnull
public static HSign getByUID(@Nonnull UUID uid) {
return HSignHandler.findByUID(uid).orElseThrow(() -> new NullPointerException("this player doesn't have a inventory!"));
}

public static HSign create(Material type, String... lines) {
/**
* Creates HSign.
*
* @param type Sign type as Material.
* @param lines Lines of sign.
* @return Created HSign.
*/
@Nonnull
public static HSign create(@Nonnull Material type, @Nonnull String... lines) {
Validate.notNull(type, "type cannot be null!");
Validate.notNull(lines, "lines cannot be null!");

try {
return (HSign) Class.forName("com.hakan.core.ui.sign.wrapper.HSign_" + HCore.getVersionString())
.getConstructor(Material.class, String[].class).newInstance(type, lines);
} catch (Exception e) {
return null;
throw new NullPointerException(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,27 @@

import javax.annotation.Nonnull;

public class HSignListener extends HListenerAdapter {
/**
* HSignListener class.
*/
public final class HSignListener extends HListenerAdapter {

/**
* Creates new instance of this class.
*
* @param plugin Main class of plugin.
*/
public HSignListener(@Nonnull JavaPlugin plugin) {
super(plugin);
}

/**
* Packet event.
*
* @param event Event.
*/
@EventHandler
public void onPacketEvent(PacketEvent event) {
public void onPacketEvent(@Nonnull PacketEvent event) {
if (event.getPacket().toString().contains("PacketPlayInUpdateSign")) {
HSignHandler.findByPlayer(event.getPlayer())
.ifPresent(hSign -> hSign.listen(event.getPlayer(), event.getPacket()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import com.hakan.core.ui.sign.HSign;
import com.hakan.core.ui.sign.HSignHandler;
import net.minecraft.server.v1_10_R1.*;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_10_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_10_R1.block.CraftSign;
import org.bukkit.craftbukkit.v1_10_R1.util.CraftMagicNumbers;
import org.bukkit.entity.Player;

import javax.annotation.Nonnull;

/**
* {@inheritDoc}
*/
Expand All @@ -19,15 +22,16 @@ public final class HSign_v1_10_R1 extends HSign {
/**
* {@inheritDoc}
*/
public HSign_v1_10_R1(Material type, String... lines) {
public HSign_v1_10_R1(@Nonnull Material type, @Nonnull String... lines) {
super(type, lines);
}

/**
* {@inheritDoc}
*/
@Override
public void open(Player player) {
public void open(@Nonnull Player player) {
Validate.notNull(player, "player cannot be null!");
BlockPosition blockPosition = new BlockPosition(player.getLocation().getBlockX(), 1, player.getLocation().getBlockZ());

PacketPlayOutBlockChange packet = new PacketPlayOutBlockChange(((CraftWorld) player.getWorld()).getHandle(), blockPosition);
Expand All @@ -48,7 +52,9 @@ public void open(Player player) {
* {@inheritDoc}
*/
@Override
public <T> void listen(Player player, T packet) {
public <T> void listen(@Nonnull Player player, @Nonnull T packet) {
Validate.notNull(player, "player cannot be null!");
Validate.notNull(packet, "packet cannot be null!");
PacketPlayInUpdateSign packetPlayInUpdateSign = (PacketPlayInUpdateSign) packet;

BlockPosition position = packetPlayInUpdateSign.a();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import com.hakan.core.ui.sign.HSign;
import com.hakan.core.ui.sign.HSignHandler;
import net.minecraft.server.v1_11_R1.*;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_11_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_11_R1.block.CraftSign;
import org.bukkit.craftbukkit.v1_11_R1.util.CraftMagicNumbers;
import org.bukkit.entity.Player;

import javax.annotation.Nonnull;

/**
* {@inheritDoc}
*/
Expand All @@ -19,15 +22,16 @@ public final class HSign_v1_11_R1 extends HSign {
/**
* {@inheritDoc}
*/
public HSign_v1_11_R1(Material type, String... lines) {
public HSign_v1_11_R1(@Nonnull Material type, @Nonnull String... lines) {
super(type, lines);
}

/**
* {@inheritDoc}
*/
@Override
public void open(Player player) {
public void open(@Nonnull Player player) {
Validate.notNull(player, "player cannot be null!");
BlockPosition blockPosition = new BlockPosition(player.getLocation().getBlockX(), 1, player.getLocation().getBlockZ());

PacketPlayOutBlockChange packet = new PacketPlayOutBlockChange(((CraftWorld) player.getWorld()).getHandle(), blockPosition);
Expand All @@ -48,7 +52,9 @@ public void open(Player player) {
* {@inheritDoc}
*/
@Override
public <T> void listen(Player player, T packet) {
public <T> void listen(@Nonnull Player player, @Nonnull T packet) {
Validate.notNull(player, "player cannot be null!");
Validate.notNull(packet, "packet cannot be null!");
PacketPlayInUpdateSign packetPlayInUpdateSign = (PacketPlayInUpdateSign) packet;

BlockPosition position = packetPlayInUpdateSign.a();
Expand Down
Loading

0 comments on commit cdcea25

Please sign in to comment.