Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Oct 24, 2020
0 parents commit 0cf95b6
Show file tree
Hide file tree
Showing 35 changed files with 1,913 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.gradle/
.nb-gradle/
.settings/
build/
eclipse/
.classpath
.nb-gradle-properties
.project
*.launch
run/
*.iml
*.ipr
*.iws
.idea/
out/
/bin/
logs/
24 changes: 24 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# The MIT License (MIT)

Copyright © 2020 Refined Mods

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Refined Storage 2

The next generation of Refined Storage.
31 changes: 31 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
plugins {
id 'fabric-loom' version '0.5-SNAPSHOT'
}

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

archivesBaseName = 'core'
group = 'com.refinedmods.refinedstorage2'
version = '1.0-SNAPSHOT'

repositories {
mavenCentral()
}

dependencies {
minecraft 'com.mojang:minecraft:1.16.3'
mappings 'net.fabricmc:yarn:1.16.3+build.17:v2'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.6.1'
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.1.0'
}

test {
useJUnitPlatform()
}

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
1 change: 1 addition & 0 deletions core/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.jvmargs=-Xmx1G
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.refinedmods.refinedstorage2.core;

public enum Action {
SIMULATE,
EXECUTE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.refinedmods.refinedstorage2.core.adapter;

import net.minecraft.block.entity.BlockEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import java.util.Optional;

public class MinecraftWorldAdapter implements WorldAdapter {
private final World world;
private final WorldIdentifier identifier;

public MinecraftWorldAdapter(World world) {
this.world = world;
this.identifier = new MinecraftWorldIdentifier(world);
}

@Override
public Optional<BlockEntity> getBlockEntity(BlockPos pos) {
return Optional.ofNullable(world.getBlockEntity(pos));
}

@Override
public WorldIdentifier getIdentifier() {
return identifier;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.refinedmods.refinedstorage2.core.adapter;

import net.minecraft.util.Identifier;
import net.minecraft.world.World;

import java.util.Objects;

public class MinecraftWorldIdentifier implements WorldIdentifier {
private final Identifier identifier;

public MinecraftWorldIdentifier(World world) {
this.identifier = world.getRegistryKey().getValue();
}

@Override
public Identifier getId() {
return identifier;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MinecraftWorldIdentifier that = (MinecraftWorldIdentifier) o;
return identifier.equals(that.identifier);
}

@Override
public int hashCode() {
return Objects.hash(identifier);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.refinedmods.refinedstorage2.core.adapter;

import net.minecraft.block.entity.BlockEntity;
import net.minecraft.util.math.BlockPos;

import java.util.Optional;

public interface WorldAdapter {
Optional<BlockEntity> getBlockEntity(BlockPos pos);

WorldIdentifier getIdentifier();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.refinedmods.refinedstorage2.core.adapter;

import net.minecraft.util.Identifier;

public interface WorldIdentifier {
Identifier getId();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.refinedmods.refinedstorage2.core.graph;

import com.refinedmods.refinedstorage2.core.adapter.WorldAdapter;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;

import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Queue;
import java.util.Set;

public class BlockEntityGraphScanner<T extends BlockEntity> implements GraphScanner<T> {
private final Class<T> blockEntityClass;

public BlockEntityGraphScanner(Class<T> blockEntityClass) {
this.blockEntityClass = blockEntityClass;
}

@Override
public GraphScannerResult<T> scanAt(WorldAdapter worldAdapter, BlockPos pos, Set<GraphEntry<T>> previousEntries) {
Set<GraphEntry<T>> allEntries = new HashSet<>();
Set<GraphEntry<T>> removedEntries = new HashSet<>(previousEntries);
Set<GraphEntry<T>> newEntries = new HashSet<>();

Queue<GraphScannerRequest> requests = new ArrayDeque<>();
requests.add(new GraphScannerRequest(worldAdapter, pos));

GraphScannerRequest request;
while ((request = requests.poll()) != null) {
handleRequest(allEntries, removedEntries, newEntries, request, requests);
}

return new GraphScannerResult<>(newEntries, removedEntries, allEntries);
}

private void handleRequest(Set<GraphEntry<T>> allEntries, Set<GraphEntry<T>> previousEntries, Set<GraphEntry<T>> newEntries, GraphScannerRequest request, Queue<GraphScannerRequest> requests) {
request.getWorldAdapter().getBlockEntity(request.getPos()).ifPresent(blockEntity -> {
if (blockEntity.getClass().isAssignableFrom(blockEntityClass)) {
GraphEntry<T> entry = new GraphEntry<>(request.getWorldAdapter().getIdentifier(), request.getPos(), (T) blockEntity);

if (allEntries.add(entry)) {
for (Direction direction : Direction.values()) {
requests.add(new GraphScannerRequest(request.getWorldAdapter(), request.getPos().offset(direction)));
}

boolean entryExistedPreviously = previousEntries.remove(entry);
if (!entryExistedPreviously) {
newEntries.add(entry);
}
}
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.refinedmods.refinedstorage2.core.graph;

import com.refinedmods.refinedstorage2.core.adapter.WorldIdentifier;
import net.minecraft.util.math.BlockPos;

import java.util.Objects;

public class GraphEntry<T> {
private final WorldIdentifier worldIdentifier;
private final BlockPos pos;
private final T element;

public GraphEntry(WorldIdentifier worldIdentifier, BlockPos pos, T element) {
this.worldIdentifier = worldIdentifier;
this.pos = pos;
this.element = element;
}

public WorldIdentifier getWorldIdentifier() {
return worldIdentifier;
}

public BlockPos getPos() {
return pos;
}

public T getElement() {
return element;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GraphEntry<?> that = (GraphEntry<?>) o;
return worldIdentifier.equals(that.worldIdentifier) && pos.equals(that.pos) && element.equals(that.element);
}

@Override
public int hashCode() {
return Objects.hash(worldIdentifier, pos, element);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.refinedmods.refinedstorage2.core.graph;

import com.refinedmods.refinedstorage2.core.adapter.WorldAdapter;
import net.minecraft.util.math.BlockPos;

import java.util.Collections;
import java.util.Set;

public interface GraphScanner<T> {
GraphScannerResult<T> scanAt(WorldAdapter worldAdapter, BlockPos pos, Set<GraphEntry<T>> previousEntries);

default GraphScannerResult<T> scanAt(WorldAdapter worldAdapter, BlockPos pos) {
return scanAt(worldAdapter, pos, Collections.emptySet());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.refinedmods.refinedstorage2.core.graph;

import com.refinedmods.refinedstorage2.core.adapter.WorldAdapter;
import net.minecraft.util.math.BlockPos;

class GraphScannerRequest {
private final WorldAdapter worldAdapter;
private final BlockPos pos;

GraphScannerRequest(WorldAdapter worldAdapter, BlockPos pos) {
this.worldAdapter = worldAdapter;
this.pos = pos;
}

public WorldAdapter getWorldAdapter() {
return worldAdapter;
}

public BlockPos getPos() {
return pos;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.refinedmods.refinedstorage2.core.graph;

import java.util.Collections;
import java.util.Set;

public class GraphScannerResult<T> {
private final Set<GraphEntry<T>> newEntries;
private final Set<GraphEntry<T>> removedEntries;
private final Set<GraphEntry<T>> allEntries;

public GraphScannerResult(Set<GraphEntry<T>> newEntries, Set<GraphEntry<T>> removedEntries, Set<GraphEntry<T>> allEntries) {
this.newEntries = Collections.unmodifiableSet(newEntries);
this.removedEntries = Collections.unmodifiableSet(removedEntries);
this.allEntries = Collections.unmodifiableSet(allEntries);
}

public Set<GraphEntry<T>> getNewEntries() {
return newEntries;
}

public Set<GraphEntry<T>> getRemovedEntries() {
return removedEntries;
}

public Set<GraphEntry<T>> getAllEntries() {
return allEntries;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.refinedmods.refinedstorage2.core.list;

import net.minecraft.item.ItemStack;

import java.util.Collection;
import java.util.Optional;
import java.util.UUID;

public interface StackList<T> {
StackListResult<T> add(T template, int amount);

Optional<StackListResult<T>> remove(T template, int amount);

Optional<ItemStack> get(T template);

Optional<ItemStack> get(UUID id);

Collection<T> getAll();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.refinedmods.refinedstorage2.core.list;

import java.util.UUID;

public class StackListResult<T> {
private final T stack;
private final int change;
private final UUID id;

public StackListResult(T stack, int change, UUID id) {
this.stack = stack;
this.change = change;
this.id = id;
}

public T getStack() {
return stack;
}

public int getChange() {
return change;
}

public UUID getId() {
return id;
}
}
Loading

0 comments on commit 0cf95b6

Please sign in to comment.