Skip to content

Instantly share code, notes, and snippets.

View DarkSeraphim's full-sized avatar

Mark Hendriks DarkSeraphim

View GitHub Profile
@DarkSeraphim
DarkSeraphim / Main.java
Created February 25, 2018 19:13
0.19% compression on bit writes of numbers :D
import java.io.Serializable;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.util.BitSet;
import java.text.NumberFormat;
import java.util.concurrent.ThreadLocalRandom;
class BitBuffer {
private BitSet buffer;
@DarkSeraphim
DarkSeraphim / PromiseRejection.md
Last active March 26, 2019 14:30
Rejected promises! ARG!

Promise rejected errors

If you're starting out with the tutorial and get errors, you probably have seen them before:

(node:12741) UnhandledPromiseRejectionWarning: O noes, an error!
(node:12741) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:12741) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

which generally leads to a tricky error that lacks any useful information as to what actually caused the error. After all, the only thing you get is the error message.

So how can we solve this? The solution is the Promise method, catch, as it allows us to get the original stack

@DarkSeraphim
DarkSeraphim / BitBuffer.java
Created February 24, 2018 23:39
BitBuffer v2
public class BitBuffer {
private ByteBuffer buffer;
private int readByteIndex;
private int readBitIndex;
private List<ToLongBiFunction<ByteBuf, Integer>> readFunctions = Arrays.asList(
(buf, index) -> buf.readByte(index), // 1 byte
(buf, index) -> buf.readShort(index), // 2 bytes
@DarkSeraphim
DarkSeraphim / BitBuffer.java
Created February 24, 2018 23:34
BitBuffer v1
public class BitBuffer {
private ByteBuffer buffer;
private int readByteIndex;
private int readBitIndex;
private List<ToLongFunction<ByteBuf>> readFunctions = Arrays.asList(
buf -> buf.readByte(), // 1 byte
buf -> buf.readShort(), // 2 bytes
public class Main {
public static void foo(String s) {
System.out.println("Got me a String!");
}
public static void foo(Object s) {
System.out.println("Got me an Object!!!");
}
public static <T> void handle(T t) {
@DarkSeraphim
DarkSeraphim / SpecUtil.java
Created November 6, 2017 21:14
Fixed processor! (with member enter for actual methods)
package me.bramhaag.annotationtest.api.util;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.Context;
import me.bramhaag.annotationtest.api.ISpec;
import me.bramhaag.annotationtest.api.MethodSpec;
@DarkSeraphim
DarkSeraphim / Bloat.java
Created November 2, 2017 00:30
Lot's of parameters are now generated!
JavacProcessingEnvironment env = (JavacProcessingEnvironment) ArmoryProcessor.environment;
JavacElements elements = env.getElementUtils();
String className = getPackageName(root) + classTree.getSimpleName();
System.out.println("Hi there");
TreeMaker maker = TreeMaker.instance(env.getContext());
Symtab symtab = Symtab.instance(env.getContext());
maker = maker.at(((JCTree.JCClassDecl) classTree).pos());
System.out.println("Size is " + classTree.getMembers().size());
JCTree[] tree = ((JCTree.JCClassDecl) classTree).defs.toArray(new JCTree[classTree.getMembers().size() + 1]);
@DarkSeraphim
DarkSeraphim / 🌲Printer.java
Created October 21, 2017 22:32
Javac AST debugging
package net.darkseraphim.armory.plugin;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.tree.JCTree;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/*
* Copyright (c) 2014, Oracle America, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
@DarkSeraphim
DarkSeraphim / LocaleFetch.java
Created May 15, 2017 09:08
Fetches the Minecraft locale files for a set of locale keys (i.e. en_gb)
private static final String LANG_URL = "http://resources.download.minecraft.net/%s/%s";
private static final String ASSET_INDEX_URL = "https://s3.amazonaws.com/Minecraft.Download/indexes/%s.json";
private static final String LOCALE_KEY = "minecraft/lang/%s.lang";
public static void loadLocales(String... languages) {
if (languages.length == 0) {
return;
}