Skip to content

Instantly share code, notes, and snippets.

View on_player_setup_blueprint.lua
script.on_event(defines.events.on_player_setup_blueprint, function(event)
local player = game.get_player(event.player_index)
local bp = player.blueprint_to_setup
if not bp.valid_for_read then
bp = player.cursor_stack
end
local bp_ents = bp.get_blueprint_entities()
if bp_ents then
local source = event.mapping.get()
for i, entity in pairs(bp_ents) do
View transfer.lua
function transfer(sourceInventory, destinationInventory, recipe, alimit, tag)
if not destinationInventory then return end
local limits = { }
if recipe then
for _, ingredient in pairs(recipe.ingredients) do
if alimit then
limits[ingredient.name] = math.min(alimit, ingredient.amount + 1)
else
limits[ingredient.name] = ingredient.amount + 1
View control.lua
script.on_event({
defines.events.on_built_entity,
defines.events.on_robot_built_entity,
defines.events.script_raised_built,
defines.events.script_raised_revive,
}, function(event)
handleEntityPlaced(event.created_entity)
end)
script.on_event({
View SortySort.java
import java.util.*;
public class SortySort {
public static String stringSorter(List<String> stringList, String sortingOrder) {
if ((stringList == null) || (stringList.isEmpty())) {
return "Given stringList or sortingOrder is empty, null or blank space";
}
if ((stringList.size() == 1)) {
return "The list contains an empty or blank space value";
}
View SplitByCurrencySymbol.md

You'll have to escape the currency symbol as it might be a reserved symbol in regex. Plus, splitting doesn't keep the symbol itself so you'll have to add it back manually:

String currency = "$"; // from your method

String escapedCurrency = Pattern.quote(currency);

String[] result = "$5".split(escapedCurrency, 2);
result[0] = currency;
View RemapCollectionKeys.java
Map<List<String>, String> map = new HashMap<>();
map.put(List.of("A", "B"), "X");
map.put(List.of("C"), "Y");
var result = map.entrySet()
.stream()
.flatMap(kv -> kv.getKey().stream().map(k -> Map.entry(k, kv.getValue())))
.collect(Collectors.toMap(k -> k.getKey(), v -> v.getValue()));
result.entrySet().forEach(System.out::println);
@akarnokd
akarnokd / main.py
Created Jan 28, 2022
Python script to convert Imperium Galactica's SMP files into WAV files
View main.py
import sys
import os.path
from os import listdir
from os.path import isfile, join
def int_to_bytes(uint):
return bytearray([
(uint & 0xFF),
((uint >> 8) & 0xFF),
View Mapper.java
static <T, R> Publisher<R> map(Publisher<T> upstream, Function<T, R> mapper) {
return downstream -> upstream.subscribe(new Mapper<>(downstream, mapper));
}
class Mapper<T, R> implements Subscriber<T> {
final Subscriber<? super R> downstream;
final Function<T, R> mapper;
View RepeatWhenExample.java
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.observers.TestObserver;
import io.reactivex.rxjava3.schedulers.TestScheduler;
public class RepeatWhenSO {
View BehaviorSubjectSignals.java
import org.junit.Test;
import io.reactivex.disposables.*;
import io.reactivex.subjects.BehaviorSubject;
public class BehaviorSubjectSignals {
@Test
public void test() throws Exception {