Skip to content

Commit

Permalink
Reformat research package
Browse files Browse the repository at this point in the history
  • Loading branch information
yuesha-yc committed Jan 18, 2025
1 parent 6d960d6 commit 7f36bf0
Show file tree
Hide file tree
Showing 129 changed files with 3,651 additions and 3,885 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
* Our own registry.
*/
public interface FHRegisteredItem {
public String getId() ;
public String getId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,79 +19,71 @@

package com.teammoeg.frostedheart.content.research;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;

import com.teammoeg.chorda.util.utility.OptionalLazy;

import net.minecraft.nbt.Tag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;

/**
* Our own registry type to reduce network and storage cost.
*
* @param <T> the generic type of registry
* @author khjxiaogu
*/
public class FHRegistry<T extends FHRegisteredItem> implements Iterable<T>{
/* private static final class RegisteredSupplier<T extends FHRegisteredItem> implements Supplier<T> {
private final String key;
private final Function<String, T> getter;
public RegisteredSupplier(String key, Function<String, T> getter) {
this.key = key;
this.getter = getter;
}
public class FHRegistry<T extends FHRegisteredItem> implements Iterable<T> {
/* private static final class RegisteredSupplier<T extends FHRegisteredItem> implements Supplier<T> {
private final String key;
private final Function<String, T> getter;
public RegisteredSupplier(String key, Function<String, T> getter) {
this.key = key;
this.getter = getter;
}
@SuppressWarnings("rawtypes")
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
RegisteredSupplier other = (RegisteredSupplier) obj;
if (key == null) {
if (other.key != null)
return false;
} else if (!key.equals(other.key))
return false;
return getter == other.getter;
}
@SuppressWarnings("rawtypes")
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
RegisteredSupplier other = (RegisteredSupplier) obj;
if (key == null) {
if (other.key != null)
return false;
} else if (!key.equals(other.key))
return false;
return getter == other.getter;
}
@Override
public T get() {
return getter.apply(key);
}
@Override
public T get() {
return getter.apply(key);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key == null) ? 0 : key.hashCode());
return result;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key == null) ? 0 : key.hashCode());
return result;
}
}*/
}*/
private ArrayList<T> items = new ArrayList<>();//registered objects
private Map<String, Integer> rnames = new HashMap<>();//registry mappings
private ArrayList<String> rnamesl = new ArrayList<>();//reverse mappings
private Map<String, OptionalLazy<T>> cache = new HashMap<>();//object cache
/*public Codec<Supplier<T>> SUPPLIER_CODEC=new CompressDifferCodec<Supplier<T>>(Codec.STRING.xmap(this::get,o->((RegisteredSupplier<T>)o).key),
Codec.INT.xmap(this::get, o->this.getIntId(((RegisteredSupplier<T>)o).key)));*/

private final Function<String, OptionalLazy<T>> cacheGen = (n) -> OptionalLazy.of(() -> getByName(n));

private Function<String, T> strLazyGetter = x -> lazyGet(x).orElse(null);
/*public Codec<Supplier<T>> SUPPLIER_CODEC=new CompressDifferCodec<Supplier<T>>(Codec.STRING.xmap(this::get,o->((RegisteredSupplier<T>)o).key),
Codec.INT.xmap(this::get, o->this.getIntId(((RegisteredSupplier<T>)o).key)));*/
private ArrayList<String> rnamesl = new ArrayList<>();//reverse mappings
private Map<String, OptionalLazy<T>> cache = new HashMap<>();//object cache

/* public static <T extends FHRegisteredItem> String serializeSupplier(Supplier<T> s) {
if (s instanceof RegisteredSupplier) {
Expand All @@ -118,6 +110,7 @@ public int hashCode() {
pb.writeVarInt(-1);
}
*/

/**
* Instantiates a new FHRegistry.<br>
*/
Expand Down Expand Up @@ -197,24 +190,28 @@ public T get(int id) {
public T get(String id) {
return get(getIntId(id));
}

public int getIntId(String obj) {
return rnames.getOrDefault(obj, -1);
return rnames.getOrDefault(obj, -1);
}

public void replace(T research) {
cache.remove(research.getId());
register(research);
cache.remove(research.getId());
register(research);
}

public int getIntId(T obj) {
return getIntId(obj.getId());
return getIntId(obj.getId());
}

/**
* Get by numeric id.
*
* @param id the id<br>
* @return by id<br>
*/
public T getById(int id) {
if(id<0||id>=items.size())return null;
if (id < 0 || id >= items.size()) return null;
return items.get(id);
}

Expand Down Expand Up @@ -270,15 +267,15 @@ public void prepareReload() {
public void register(T item) {
String lid = item.getId();
int index = getIntId(lid);

ensure();
if (index == -1) {
rnames.put(item.getId(), rnamesl.size());
// System.out.println(lid+" registered index"+rnamesl.size()+"");
// System.out.println(lid+" registered index"+rnamesl.size()+"");
rnamesl.add(item.getId());
items.add(item);
} else {
//System.out.println(lid+" re-registered index"+index+"");
//System.out.println(lid+" re-registered index"+index+"");
items.set(index, item);
}
}
Expand Down Expand Up @@ -322,8 +319,8 @@ public ListTag serialize() {
return () -> null;
}*/

@Override
public Iterator<T> iterator() {
return items.stream().filter(Objects::nonNull).iterator();
}
@Override
public Iterator<T> iterator() {
return items.stream().filter(Objects::nonNull).iterator();
}
}
Loading

0 comments on commit 7f36bf0

Please sign in to comment.