Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.api.filesystem.IWritableMount;
import dan200.computercraft.api.media.IMedia;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.pocket.IPocketUpgrade;
import dan200.computercraft.shared.PocketUpgrades;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.core.ServerComputerRegistry;
import dan200.computercraft.shared.computer.core.ServerContext;
Expand All @@ -23,6 +26,7 @@
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.Container;
Expand All @@ -47,6 +51,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Map;

public class SmartGlassesItem extends ArmorItem implements IComputerItem, IMedia {

Expand Down Expand Up @@ -83,6 +88,9 @@ public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable D

private boolean tick(ItemStack stack, Level world, Entity entity, SmartGlassesComputer computer) {
computer.setLevel((ServerLevel) world);
if (entity != null) {
computer.setPosition(entity.blockPosition());
}

boolean changed = false;

Expand Down Expand Up @@ -118,16 +126,23 @@ private boolean tick(ItemStack stack, Level world, Entity entity, SmartGlassesCo
computer.setStack(stack);
}

for (Map.Entry<ResourceLocation, IPeripheral> e : computer.getUpgrades().entrySet()) {
IPocketUpgrade upgrade = PocketUpgrades.instance().get(e.getKey().toString());
if (upgrade != null) {
upgrade.update(computer, e.getValue());
}
}

return changed;
}

@Override
public void inventoryTick(@NotNull ItemStack stack, @NotNull Level world, @NotNull Entity entity, int slotNum, boolean selected) {
LazyOptional<IItemHandler> optItemHandler = stack.getCapability(ForgeCapabilities.ITEM_HANDLER);
SmartGlassesItemHandler itemHandler = (SmartGlassesItemHandler) optItemHandler.orElse(null);
for(int slot = 0; slot < itemHandler.getSlots(); slot++) {
for (int slot = 0; slot < itemHandler.getSlots(); slot++) {
ItemStack itemStack = itemHandler.getStackInSlot(slot);
if(itemStack.getItem() instanceof IModuleItem iModuleItem) {
if (itemStack.getItem() instanceof IModuleItem iModuleItem) {
SmartGlassesAccess glassesAccess = null;
IModule module = null;
if (!world.isClientSide) {
Expand Down Expand Up @@ -155,7 +170,9 @@ public void inventoryTick(@NotNull ItemStack stack, @NotNull Level world, @NotNu

@Override
public boolean onEntityItemUpdate(ItemStack stack, ItemEntity entity) {
if (entity.level.isClientSide || entity.level.getServer() == null) return false;
if (entity.level.isClientSide || entity.level.getServer() == null) {
return false;
}

SmartGlassesComputer computer = getServerComputer(entity.level.getServer(), stack);
if (computer != null && tick(stack, entity.level, entity, computer)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@
import de.srendi.advancedperipherals.common.smartglasses.modules.IModule;
import de.srendi.advancedperipherals.common.smartglasses.modules.IModuleItem;
import de.srendi.advancedperipherals.common.smartglasses.modules.ModulePeripheral;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;
import com.google.common.collect.ImmutableMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;

/**
* Basically just a {@link dan200.computercraft.shared.pocket.core.PocketServerComputer} but with some changes
Expand All @@ -40,20 +41,18 @@ public class SmartGlassesComputer extends ServerComputer implements IPocketAcces
@Nullable
private SmartGlassesItemHandler itemHandler = null;
@NotNull
private final ModulePeripheral peripheral;
private final ModulePeripheral modulePeripheral;

private int lightColour = -1;
private boolean lightChanged = false;
private boolean isDirty = false;
private boolean isDirty = true;

private final Set<ServerPlayer> tracking = new HashSet<>();
private Map<ResourceLocation, IPeripheral> upgrades = Collections.emptyMap();
private final Map<Integer, IModule> modules = new HashMap<>();

public SmartGlassesComputer(ServerLevel world, int computerID, @Nullable String label, ComputerFamily family) {
super(world, computerID, label, family, 39, 13);
this.addAPI(new SmartGlassesAPI());
peripheral = new ModulePeripheral(this);
this.setPeripheral(ComputerSide.BACK, peripheral);
this.modulePeripheral = new ModulePeripheral(this);
this.setPeripheral(ComputerSide.BACK, this.modulePeripheral);
}

@Nullable
Expand All @@ -65,7 +64,7 @@ public Entity getEntity() {

if (entity instanceof Player player) {
Inventory inventory = player.getInventory();
if (inventory.items.contains(stack) || inventory.armor.contains(stack) || inventory.offhand.contains(stack)) {
if (inventory.contains(stack)) {
return player;
}
return null;
Expand All @@ -76,6 +75,16 @@ public Entity getEntity() {
return null;
}

@Override
public ServerLevel getLevel() {
return this.entity == null ? super.getLevel() : (ServerLevel) this.entity.getCommandSenderWorld();
}

@Override
public BlockPos getPosition() {
return this.entity == null ? super.getPosition() : this.entity.blockPosition();
}

@Override
public int getColour() {
return 0;
Expand All @@ -96,20 +105,11 @@ public ItemStack getStack() {

@Override
public int getLight() {
return lightColour;
return 0;
}

@Override
public void setLight(int colour) {
if (colour < 0 || colour > 0xFFFFFF) {
colour = -1;
}

if (lightColour == colour) {
return;
}
lightColour = colour;
lightChanged = true;
}

public void setItemHandler(@Nullable SmartGlassesItemHandler itemHandler) {
Expand Down Expand Up @@ -139,34 +139,35 @@ public void updateUpgradeNBTData() {

@Override
public void invalidatePeripheral() {
updatePeripheralsAndModules(this.itemHandler);
}

@Override
@NotNull
public Map<ResourceLocation, IPeripheral> getUpgrades() {
return Collections.emptyMap();
return this.upgrades;
}

@Override
public void setPeripheral(ComputerSide side, IPeripheral peripheral) {
super.setPeripheral(side, peripheral);
}

public void updatePeripheralsAndModules(SmartGlassesItemHandler itemHandler) {
for (int slot = 0; slot < 5; slot++) {
Set<ResourceLocation> upgradesIdSet = new HashSet<>();
ImmutableMap.Builder<ResourceLocation, IPeripheral> upgradesBuilder = new ImmutableMap.Builder<>();
for (int slot = 0; slot < SmartGlassesItemHandler.PERIPHERAL_SLOTS; slot++) {
ComputerSide side = SmartGlassesSlot.indexToSide(slot);
ItemStack peripheralItem = itemHandler.getStackInSlot(slot);
if (!peripheralItem.isEmpty()) {
IPocketUpgrade upgrade = PocketUpgrades.instance().get(peripheralItem);
if (upgrade != null) {
IPeripheral peripheral = upgrade.createPeripheral(smartGlassesAccess);
if (peripheral != null) {
setPeripheral(SmartGlassesSlot.indexToSide(slot), peripheral);
continue;
}
}
IPocketUpgrade upgrade = PocketUpgrades.instance().get(peripheralItem);
IPeripheral peripheral = upgrade != null ? upgrade.createPeripheral(smartGlassesAccess) : null;
setPeripheral(side, peripheral);
if (peripheral != null && upgradesIdSet.add(upgrade.getUpgradeID())) {
upgradesBuilder.put(upgrade.getUpgradeID(), peripheral);
}
setPeripheral(SmartGlassesSlot.indexToSide(slot), null);
}
for (int slot = 5; slot < 11; slot++) {
this.upgrades = upgradesBuilder.build();
for (int slot = SmartGlassesItemHandler.PERIPHERAL_SLOTS; slot < SmartGlassesItemHandler.SLOTS; slot++) {
ItemStack peripheralItem = itemHandler.getStackInSlot(slot);
IModule oldModule = modules.get(slot);
if (!peripheralItem.isEmpty() && peripheralItem.getItem() instanceof IModuleItem module) {
Expand All @@ -175,32 +176,25 @@ public void updatePeripheralsAndModules(SmartGlassesItemHandler itemHandler) {
continue;
}
modules.put(slot, newModule);
peripheral.updateModules();
setPeripheral(ComputerSide.BACK, null);
setPeripheral(ComputerSide.BACK, peripheral);
} else if (oldModule != null) {
oldModule.onUnequipped(smartGlassesAccess);
modules.remove(slot);
}
}
this.modulePeripheral.updateModules();
setPeripheral(ComputerSide.BACK, null);
setPeripheral(ComputerSide.BACK, this.modulePeripheral);
if (this.entity instanceof Player player) {
player.getInventory().setChanged();
}
}

@Override
public void tickServer() {
super.tickServer();

// Find any players which have gone missing and remove them from the tracking list.
tracking.removeIf(player -> !player.isAlive() || player.level != getLevel());

// And now find any new players, add them to the tracking list, and broadcast state where appropriate.
boolean sendState = hasOutputChanged() || lightChanged;
lightChanged = false;
if (sendState) {
tracking.addAll(getLevel().players());
}

if (isDirty()) {
updatePeripheralsAndModules(this.itemHandler);
invalidatePeripheral();
isDirty = false;
}

Expand All @@ -210,14 +204,21 @@ public void tickServer() {
}

public void setEntity(@Nullable Entity entity) {
if (this.entity == entity) {
return;
}
this.entity = entity;
if (entity == null) {
return;
}
this.setLevel((ServerLevel) this.entity.getCommandSenderWorld());
this.setPosition(this.entity.blockPosition());
}

public Map<Integer, IModule> getModules() {
return modules;
}


@Override
protected void onRemoved() {
super.onRemoved();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package de.srendi.advancedperipherals.common.smartglasses;

import dan200.computercraft.api.pocket.IPocketUpgrade;
import dan200.computercraft.shared.PocketUpgrades;
import de.srendi.advancedperipherals.common.items.SmartGlassesItem;
import de.srendi.advancedperipherals.common.smartglasses.modules.IModuleItem;
import net.minecraft.core.NonNullList;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.ContainerHelper;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemHandlerHelper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import javax.annotation.Nonnull;

public class SmartGlassesItemHandler implements IItemHandlerModifiable {

private static final int SLOTS = 12;
public static final int SLOTS = 11;
public static final int PERIPHERAL_SLOTS = 5;

private final ItemStack glasses;
@Nullable
Expand All @@ -40,7 +47,34 @@ public int getSlotLimit(int slot) {

@Override
public boolean isItemValid(int slot, @NotNull ItemStack stack) {
return !(stack.getItem() instanceof SmartGlassesItem);
if (stack.getItem() instanceof SmartGlassesItem) {
return false;
}
List<ItemStack> items = this.loadItems();
if (slot < PERIPHERAL_SLOTS) {
IPocketUpgrade upgrade = PocketUpgrades.instance().get(stack);
if (upgrade == null) {
return false;
}
ResourceLocation id = upgrade.getUpgradeID();
for (int i = 0; i < PERIPHERAL_SLOTS; i++) {
IPocketUpgrade u = PocketUpgrades.instance().get(items.get(i));
if (u != null && u.getUpgradeID().equals(id)) {
return false;
}
}
return true;
}
Item item = stack.getItem();
if (!(item instanceof IModuleItem module)) {
return false;
}
for (int i = PERIPHERAL_SLOTS; i < SLOTS; i++) {
if (items.get(i).getItem() == item) {
return false;
}
}
return true;
}

@Override
Expand All @@ -53,15 +87,11 @@ public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate
return stack;
}
ItemStack existing = getStackInSlot(slot);
int limit = getSlotLimit(slot);

if (!existing.isEmpty()) {
if (!ItemHandlerHelper.canItemStacksStack(stack, existing)) {
return stack;
}
limit -= existing.getCount();
return stack;
}

int limit = getSlotLimit(slot);
if (limit <= 0) {
return stack;
}
Expand Down Expand Up @@ -117,7 +147,7 @@ public ItemStack getStackInSlot(int slot) {
@Override
public void setStackInSlot(int slot, @NotNull ItemStack stack) {
NonNullList<ItemStack> items = loadItems();
if (stack.equals(items.get(slot))) {
if (ItemStack.isSameItemSameTags(stack, items.get(slot))) {
return;
}
items.set(slot, stack);
Expand Down