Moving and stopping
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package io.github.skippyall.minions.module;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
import eu.pb4.polymer.core.api.other.PolymerComponent;
|
||||
import io.github.skippyall.minions.MinionRegistries;
|
||||
import io.github.skippyall.minions.Minions;
|
||||
import io.github.skippyall.minions.program.instruction.InstructionType;
|
||||
import net.minecraft.component.ComponentType;
|
||||
import net.minecraft.registry.Registries;
|
||||
import net.minecraft.registry.Registry;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record MinionModule(List<InstructionType<?>> instructions) {
|
||||
public static final Codec<MinionModule> CODEC = RecordCodecBuilder.create(instance ->
|
||||
instance.group(
|
||||
MinionRegistries.INSTRUCTION_TYPES.getCodec().listOf().fieldOf("instructions").forGetter(MinionModule::instructions)
|
||||
).apply(instance, MinionModule::new)
|
||||
);
|
||||
|
||||
public static final ComponentType<MinionModule> COMPONENT_TYPE = ComponentType.<MinionModule>builder().codec(CODEC).build();
|
||||
|
||||
public static final MinionModule EMPTY = new MinionModule(List.of());
|
||||
|
||||
public MinionModule(List<InstructionType<?>> instructions) {
|
||||
this.instructions = List.copyOf(instructions);
|
||||
}
|
||||
|
||||
public static void register() {
|
||||
Registry.register(Registries.DATA_COMPONENT_TYPE, Identifier.of(Minions.MOD_ID, "minion_module"), COMPONENT_TYPE);
|
||||
PolymerComponent.registerDataComponent(COMPONENT_TYPE);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package io.github.skippyall.minions.module;
|
||||
|
||||
import io.github.skippyall.minions.minion.fakeplayer.MinionFakePlayer;
|
||||
import io.github.skippyall.minions.program.instruction.InstructionType;
|
||||
import net.minecraft.inventory.Inventories;
|
||||
import net.minecraft.inventory.SimpleInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.screen.SimpleNamedScreenHandlerFactory;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.storage.ReadView;
|
||||
import net.minecraft.storage.WriteView;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class ModuleInventory extends SimpleInventory {
|
||||
private final Set<MinionModule> modules = new HashSet<>();
|
||||
public ModuleInventory() {
|
||||
super(27);
|
||||
}
|
||||
|
||||
public static void openModuleInventory(ServerPlayerEntity player, MinionFakePlayer minion) {
|
||||
player.openHandledScreen(new SimpleNamedScreenHandlerFactory((syncId, playerInventory, player2) -> new ModuleInventoryScreenHandler(syncId, playerInventory, minion.getModuleInventory()), Text.translatable("minions.gui.modules.title", minion.getName())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxCountPerStack() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(int slot, ItemStack stack) {
|
||||
return (stack.getCount() <= getMaxCountPerStack()) && stack.contains(MinionModule.COMPONENT_TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markDirty() {
|
||||
super.markDirty();
|
||||
updateModules();
|
||||
}
|
||||
|
||||
public void updateModules() {
|
||||
modules.clear();
|
||||
for (ItemStack heldStack : heldStacks) {
|
||||
MinionModule module = heldStack.get(MinionModule.COMPONENT_TYPE);
|
||||
if(module != null) {
|
||||
modules.add(module);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void readData(ReadView view) {
|
||||
Inventories.readData(view, heldStacks);
|
||||
updateModules();
|
||||
}
|
||||
|
||||
public void writeData(WriteView view) {
|
||||
Inventories.writeData(view, heldStacks);
|
||||
}
|
||||
|
||||
public boolean hasModule(MinionModule module) {
|
||||
return modules.contains(module);
|
||||
}
|
||||
|
||||
public Collection<MinionModule> getModules() {
|
||||
return modules;
|
||||
}
|
||||
|
||||
public List<InstructionType<?>> getAllInstructions() {
|
||||
ArrayList<InstructionType<?>> instructionTypes = new ArrayList<>();
|
||||
for(MinionModule module : modules) {
|
||||
instructionTypes.addAll(module.instructions());
|
||||
}
|
||||
return instructionTypes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package io.github.skippyall.minions.module;
|
||||
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.screen.GenericContainerScreenHandler;
|
||||
import net.minecraft.screen.ScreenHandler;
|
||||
import net.minecraft.screen.ScreenHandlerType;
|
||||
import net.minecraft.screen.slot.Slot;
|
||||
|
||||
public class ModuleInventoryScreenHandler extends ScreenHandler {
|
||||
private final int rows = 3;
|
||||
private final ModuleInventory inventory;
|
||||
public ModuleInventoryScreenHandler(int syncId, ModuleInventory inventory) {
|
||||
super(ScreenHandlerType.GENERIC_9X3, syncId);
|
||||
this.inventory = inventory;
|
||||
}
|
||||
|
||||
public ModuleInventoryScreenHandler(int syncId, PlayerInventory playerInventory, ModuleInventory inventory) {
|
||||
super(ScreenHandlerType.GENERIC_9X3, syncId);
|
||||
int k;
|
||||
int j;
|
||||
GenericContainerScreenHandler.checkSize(inventory, 3 * 9);
|
||||
this.inventory = inventory;
|
||||
inventory.onOpen(playerInventory.player);
|
||||
int i = (rows - 4) * 18;
|
||||
for (j = 0; j < rows; ++j) {
|
||||
for (k = 0; k < 9; ++k) {
|
||||
this.addSlot(new Slot(inventory, k + j * 9, 8 + k * 18, 18 + j * 18) {
|
||||
@Override
|
||||
public boolean canInsert(ItemStack stack) {
|
||||
return super.canInsert(stack) && inventory.isValid(getIndex(), stack);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
for (j = 0; j < 3; ++j) {
|
||||
for (k = 0; k < 9; ++k) {
|
||||
this.addSlot(new Slot(playerInventory, k + j * 9 + 9, 8 + k * 18, 103 + j * 18 + i));
|
||||
}
|
||||
}
|
||||
for (j = 0; j < 9; ++j) {
|
||||
this.addSlot(new Slot(playerInventory, j, 8 + j * 18, 161 + i));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canUse(PlayerEntity player) {
|
||||
return this.inventory.canPlayerUse(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack quickMove(PlayerEntity player, int slot) {
|
||||
ItemStack itemStack = ItemStack.EMPTY;
|
||||
Slot slot2 = this.slots.get(slot);
|
||||
if (slot2 != null && slot2.hasStack()) {
|
||||
ItemStack itemStack2 = slot2.getStack();
|
||||
itemStack = itemStack2.copy();
|
||||
if (slot < this.rows * 9 ? !this.insertItem(itemStack2, this.rows * 9, this.slots.size(), true) : !this.insertItem(itemStack2, 0, this.rows * 9, false)) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
if (itemStack2.isEmpty()) {
|
||||
slot2.setStack(ItemStack.EMPTY);
|
||||
} else {
|
||||
slot2.markDirty();
|
||||
}
|
||||
}
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClosed(PlayerEntity player) {
|
||||
super.onClosed(player);
|
||||
this.inventory.onClose(player);
|
||||
}
|
||||
|
||||
public ModuleInventory getInventory() {
|
||||
return inventory;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user