Skip to content

Instantly share code, notes, and snippets.

View Koboo's full-sized avatar
🔬
....

Koboo

🔬
....
View GitHub Profile
@Koboo
Koboo / PlayerMoveEvent.java
Created September 18, 2022 13:45
"Infinite" room in bukkit through player move event
@EventHandler(priority = EventPriority.HIGHEST)
public void onMove(PlayerMoveEvent event) {
Location from = event.getFrom();
Location to = event.getTo();
if(!from.getWorld().getName().equalsIgnoreCase(to.getWorld().getName())) {
return;
}
@Koboo
Koboo / GenericClass.java
Created August 23, 2022 09:08
Get class of generic object from java class
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class GenericClass<T> {
public GenericClass() {
Type superclass = this.getClass().getGenericSuperclass();
Type genericClass = ((ParameterizedType) superclass).getActualTypeArguments()[0];
}
@Koboo
Koboo / SwitchClass.java
Last active August 22, 2022 09:47
Switch and case class of an object
import java.util.Optional;
import java.util.function.Consumer;
@SuppressWarnings("all")
public class SwitchClass {
static public <T> void cswitch(Object object, Consumer... consumers) {
for (Consumer consumer : consumers) {
consumer.accept(object);
@Koboo
Koboo / EncryptUtils.java
Created August 22, 2022 09:41
Very simple Encryption utility
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
public class EncryptUtils {
private static final String ALGORITHM = "AES";
@Koboo
Koboo / Pagifier.java
Created August 22, 2022 09:30
Pagifier for generic Objects
public class Pagifier<T> {
private final int maxItemsPerPage;
private final List<List<T>> allPages;
public Pagifier(int maxItemsPerPage) {
this.maxItemsPerPage = maxItemsPerPage;
allPages = new LinkedList<>();
allPages.add(new LinkedList<>());
@Koboo
Koboo / TimeStamp.java
Created August 22, 2022 09:28
Simple TmeStamp class to measure times
import java.util.concurrent.TimeUnit;
public class TimeStamp {
private static final String ZERO = "0";
private static final String MILLIS = "ms";
private static final String NANOS = "ns";
private static final String SECONDS = "s";
private static final String ZERO_MILLIS = ZERO + MILLIS;
@Koboo
Koboo / StringGenerator.java
Created May 5, 2022 11:11
Create a random string by a given length and characters
public class StringGenerator {
public static String createRandomString(int length, String characters) {
StringBuilder stringBuilder = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int index = (int) (characters.length() * Math.random());
stringBuilder.append(characters.charAt(index));
}
return stringBuilder.toString();
}
@Koboo
Koboo / Clipboard.java
Created January 12, 2022 12:59
Viewless Clipboard utility for vaadin flow web-apps
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.notification.NotificationVariant;
public class Clipboard {
public Clipboard() {
String javaScriptFunction = "window.copyToClipboard = (str) => {\n"
+ " const textarea = document.createElement(\"textarea\");\n"
+ " textarea.value = str;\n"
@Koboo
Koboo / LongId.java
Created October 5, 2021 06:45
Simple utility class to automatically generate long ids
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Predicate;
public class LongId {
private final AtomicLong atomicLong = new AtomicLong(0);
public long generateId(Predicate<Long> exists) {
if(this.atomicLong.get() == Integer.MAX_VALUE) {
this.atomicLong.set(0);
@Koboo
Koboo / BsonToBinary.java
Last active February 15, 2023 19:01
Just a static utility class to convert BSON Documents to byte-array and vice versa
import org.bson.BsonBinaryReader;
import org.bson.BsonBinaryWriter;
import org.bson.Document;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.DocumentCodec;
import org.bson.codecs.EncoderContext;
import org.bson.io.BasicOutputBuffer;
import java.nio.ByteBuffer;