Change back id to being UUIDs

This commit is contained in:
DBotThePony 2021-08-27 14:33:36 +07:00
parent 05bed3a737
commit 998ce95f1c
Signed by: DBot
GPG Key ID: DCC23B5715498507
6 changed files with 35 additions and 52 deletions

View File

@ -1,5 +1,6 @@
package ru.dbotthepony.mc.otm.capability.drive;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
@ -7,11 +8,16 @@ import net.minecraftforge.common.util.INBTSerializable;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;
import java.util.UUID;
import java.util.function.Predicate;
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public interface IMatteryDrive {
record StoredStack(ItemStack stack, UUID id) {};
List<StoredStack> getItems();
boolean isDirty();
@ -22,32 +28,27 @@ public interface IMatteryDrive {
* @param item
* @return all items belonging to passed class
*/
@Nonnull
List<StoredStack> findItems(@Nonnull Item item);
List<StoredStack> findItems(Item item);
/**
* @param stack
* @return all items that match this itemstack
*/
@Nonnull
List<StoredStack> findItems(@Nonnull ItemStack stack);
List<StoredStack> findItems(ItemStack stack);
@Nullable
StoredStack getItem(int id);
StoredStack getItem(UUID id);
int getStoredCount();
int getCapacity();
@Nonnull
ItemStack insertItem(@Nonnull ItemStack item, boolean simulate);
ItemStack insertItem(ItemStack item, boolean simulate);
@Nonnull
default ItemStack extractItem(int id, int amount, boolean simulate) {
default ItemStack extractItem(UUID id, int amount, boolean simulate) {
return extractItem(id, amount, true, simulate);
}
@Nonnull
ItemStack extractItem(int id, int amount, boolean obey_stack_size, boolean simulate);
ItemStack extractItem(UUID id, int amount, boolean obey_stack_size, boolean simulate);
// not extending INBTSerializable to avoid serializing it as forgecaps
CompoundTag serializeNBT();

View File

@ -16,18 +16,13 @@ import java.util.*;
public class MatteryDrive implements IMatteryDrive {
protected final HashMap<Item, List<StoredStack>> items = new HashMap<>();
protected final HashMap<Integer, StoredStack> items_by_id = new HashMap<>();
protected final HashMap<UUID, StoredStack> items_by_id = new HashMap<>();
protected boolean dirty = false;
protected int different_stacks = 0;
protected int stored = 0;
protected int max_different_stacks;
protected int capacity;
protected int next_item_id = 0;
protected int getNextID() {
return next_item_id++;
}
public MatteryDrive(int capacity, int max_different_stacks) {
this.capacity = capacity;
@ -122,7 +117,7 @@ public class MatteryDrive implements IMatteryDrive {
@Nullable
@Override
public StoredStack getItem(int id) {
public StoredStack getItem(UUID id) {
return items_by_id.get(id);
}
@ -158,7 +153,7 @@ public class MatteryDrive implements IMatteryDrive {
different_stacks++;
var copy = item.copy();
copy.setCount(max_insert);
var state = new StoredStack(copy, getNextID());
var state = new StoredStack(copy, UUID.randomUUID());
listing.add(state);
items_by_id.put(state.id(), state);
markDirty();
@ -171,7 +166,7 @@ public class MatteryDrive implements IMatteryDrive {
@Nonnull
@Override
public ItemStack extractItem(int id, int amount, boolean obey_stack_size, boolean simulate) {
public ItemStack extractItem(UUID id, int amount, boolean obey_stack_size, boolean simulate) {
var get = items_by_id.get(id);
if (get == null)
@ -204,7 +199,6 @@ public class MatteryDrive implements IMatteryDrive {
public CompoundTag serializeNBT() {
final var compound = new CompoundTag();
compound.putInt("next_item_id", next_item_id);
compound.putInt("capacity", capacity);
compound.putInt("max_different_stacks", max_different_stacks);
@ -224,7 +218,7 @@ public class MatteryDrive implements IMatteryDrive {
stack_list.add(stack_nbt);
stack_nbt.putInt("count", stack.stack().getCount());
stack_nbt.putInt("id", stack.id());
stack_nbt.putLongArray("id", new long[] { stack.id().getMostSignificantBits(), stack.id().getLeastSignificantBits() });
if (stack.stack().getTag() != null) {
stack_nbt.put("data", stack.stack().getTag());
@ -244,7 +238,6 @@ public class MatteryDrive implements IMatteryDrive {
stored = 0;
different_stacks = 0;
next_item_id = nbt.getInt("next_item_id");
capacity = nbt.getInt("capacity");
max_different_stacks = nbt.getInt("max_different_stacks");
@ -263,7 +256,7 @@ public class MatteryDrive implements IMatteryDrive {
for (var _stack : stack_list) {
if (_stack instanceof CompoundTag stack) {
var count = stack.getInt("count");
var id = stack.getInt("id");
var id = stack.getLongArray("id");
var data = stack.get("data");
var itemstack = new ItemStack(item, count);
@ -274,7 +267,7 @@ public class MatteryDrive implements IMatteryDrive {
stored += count;
different_stacks += 1;
var state = new StoredStack(itemstack, id);
var state = new StoredStack(itemstack, id.length == 2 ? new UUID(id[0], id[1]) : UUID.randomUUID());
map_list.add(state);
items_by_id.put(state.id(), state);
}

View File

@ -1,8 +0,0 @@
package ru.dbotthepony.mc.otm.capability.drive;
import net.minecraft.world.item.ItemStack;
import java.util.UUID;
public record StoredStack(ItemStack stack, int id) {
}

View File

@ -3,19 +3,16 @@ package ru.dbotthepony.mc.otm.menu;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.ItemStack;
import ru.dbotthepony.mc.otm.Registry;
import ru.dbotthepony.mc.otm.block.entity.BlockEntityDriveViewer;
import ru.dbotthepony.mc.otm.block.entity.BlockEntityMatteryPowered;
import ru.dbotthepony.mc.otm.capability.MatteryCapability;
import ru.dbotthepony.mc.otm.capability.drive.IMatteryDrive;
import ru.dbotthepony.mc.otm.capability.drive.NetworkedItemView;
import ru.dbotthepony.mc.otm.menu.data.NetworkedItemView;
import ru.dbotthepony.mc.otm.menu.data.UUIDDataContainer;
import ru.dbotthepony.mc.otm.menu.slot.MatterySlot;
import javax.annotation.Nullable;
import java.util.UUID;
public class DriveViewerMenu extends PoweredMatteryMenu {
public UUIDDataContainer view_uuid;

View File

@ -1,4 +1,4 @@
package ru.dbotthepony.mc.otm.capability.drive;
package ru.dbotthepony.mc.otm.menu.data;
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
@ -11,7 +11,6 @@ import net.minecraftforge.fmllegacy.network.PacketDistributor;
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
import ru.dbotthepony.mc.otm.network.MatteryNetworking;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -19,9 +18,9 @@ import java.util.UUID;
import java.util.function.Supplier;
public class NetworkedItemView {
record TrackedState(int id, int id_upstream, ItemStack stack) {
TrackedState(int id, ItemStack stack) {
this(id, 0, stack);
public record ViewedItem(int id, ItemStack stack, UUID id_upstream) {
public ViewedItem(int id, ItemStack stack) {
this(id, stack, null);
}
}
@ -112,7 +111,7 @@ public class NetworkedItemView {
throw new IllegalStateException("Item tracker " + id + " already has stack with id of " + stack_id);
}
get.state.put(stack_id, new TrackedState(stack_id, stack));
get.state.put(stack_id, new ViewedItem(stack_id, stack));
get.clearCache();
});
}
@ -194,11 +193,14 @@ public class NetworkedItemView {
public final boolean remote;
protected int next_stack_id = 0;
protected final HashMap<Integer, TrackedState> state = new HashMap<>();
protected final HashMap<Integer, TrackedState> upstream_state = new HashMap<>();
protected final HashMap<Integer, ViewedItem> state = new HashMap<>();
protected final HashMap<UUID, ViewedItem> upstream_state = new HashMap<>();
protected final ArrayList<Object> backlog = new ArrayList<>();
// normally, filled only on server
private static final HashMap<UUID, NetworkedItemView> TRACKERS_LOCAL = new HashMap<>();
// normally, filled only on client
private static final HashMap<UUID, NetworkedItemView> TRACKERS_REMOTE = new HashMap<>();
public static NetworkedItemView getLocal(UUID id) {
@ -248,11 +250,11 @@ public class NetworkedItemView {
return view_cache = list;
}
public void addItem(ItemStack stack, int id_upstream) {
public void addItem(ItemStack stack, UUID id_upstream) {
if (upstream_state.containsKey(id_upstream))
throw new IllegalStateException("Already tracking ItemStack with upstream id " + id_upstream + "!");
var state = new TrackedState(next_stack_id++, id_upstream, stack);
var state = new ViewedItem(next_stack_id++, stack, id_upstream);
this.state.put(state.id, state);
this.upstream_state.put(id_upstream, state);
@ -263,7 +265,7 @@ public class NetworkedItemView {
clearCache();
}
public void changeItem(int id_upstream, int new_count) {
public void changeItem(UUID id_upstream, int new_count) {
var get = upstream_state.get(id_upstream);
if (get == null)
@ -278,7 +280,7 @@ public class NetworkedItemView {
clearCache();
}
public void removeItem(int id_upstream) {
public void removeItem(UUID id_upstream) {
var get = upstream_state.get(id_upstream);
if (get == null)
@ -295,7 +297,6 @@ public class NetworkedItemView {
}
public void clear() {
OverdriveThatMatters.LOGGER.info("Clear called");
clearCache();
upstream_state.clear();
state.clear();
@ -322,7 +323,6 @@ public class NetworkedItemView {
for (var packet : backlog) {
MatteryNetworking.CHANNEL.send(consumer, packet);
OverdriveThatMatters.LOGGER.info("Send packet {}", packet);
}
backlog.clear();

View File

@ -5,7 +5,7 @@ import net.minecraftforge.fmllegacy.network.NetworkDirection;
import net.minecraftforge.fmllegacy.network.NetworkRegistry;
import net.minecraftforge.fmllegacy.network.simple.SimpleChannel;
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
import ru.dbotthepony.mc.otm.capability.drive.NetworkedItemView;
import ru.dbotthepony.mc.otm.menu.data.NetworkedItemView;
import ru.dbotthepony.mc.otm.network.android.*;
import java.util.Optional;