From b82c7ba0511e93d2e71391c71c8335dacec4eecd Mon Sep 17 00:00:00 2001 From: Nikolay Savenko Date: Mon, 30 Nov 2020 20:16:05 +0300 Subject: [PATCH 1/6] updated StorageInterface --- declarations/lib/StorageInterface.d.ts | 31 +++------ src/lib/StorageInterface.js | 95 ++++++++++++++++---------- 2 files changed, 68 insertions(+), 58 deletions(-) diff --git a/declarations/lib/StorageInterface.d.ts b/declarations/lib/StorageInterface.d.ts index 728cc41..b31d00d 100644 --- a/declarations/lib/StorageInterface.d.ts +++ b/declarations/lib/StorageInterface.d.ts @@ -1,32 +1,21 @@ - +/// declare namespace StorageInterface { - const data: {}; - const directionsBySide: { - x: number; - y: number; - z: number; - }[]; - function getRelativeCoords(coords: Vector | TileEntity, side: number): { - x: number; - y: number; - z: number; - }; - function newInstance(id: number, tileEntity: TileEntity): { - tileEntity: TileEntity; - container: UI.Container; - liquidStorage: any; - }; + const directionsBySide: Vector[]; + function getRelativeCoords(coords: Vector, side: number): Vector; + function setSlotMaxStackPolicy(container: ItemContainer, slotName: string, maxCount: number): void; + function setSlotValidatePolicy(container: ItemContainer, slotName: string, func: (name: string, id: number, amount: number, data: number, extra: ItemExtraData, container: ItemContainer, playerUid: number) => boolean): void; + function setGlobalValidatePolicy(container: ItemContainer, func: (name: string, id: number, amount: number, data: number, extra: ItemExtraData, container: ItemContainer, playerUid: number) => boolean): void; function createInterface(id: number, interface: any): void; function addItemToSlot(item: ItemInstance, slot: ItemInstance, count: number): number; - function getNearestContainers(coords: Vector | TileEntity, side: number, excludeSide?: boolean): {}; - function getNearestLiquidStorages(coords: Vector | TileEntity, side: number, excludeSide?: boolean): {}; + function getNearestContainers(coords: Vector, side: number, excludeSide?: boolean, region?: BlockSource): {}; + function getNearestLiquidStorages(coords: Vector, side: number, excludeSide?: boolean, region?: BlockSource): {}; function putItems(items: ItemInstance[], containers: any): void; function putItemToContainer(item: ItemInstance, container: NativeTileEntity | UI.Container | ItemContainer, side: number, maxCount?: number): number; - function extractItemsFromContainer(inputTile: TileEntity, container: NativeTileEntity | UI.Container, side: number, maxCount?: number, oneStack?: boolean): number; + function extractItemsFromContainer(inputTile: TileEntity, container: NativeTileEntity | UI.Container | ItemContainer, side: number, maxCount?: number, oneStack?: boolean): number; function extractLiquid(liquid: string, maxAmount: number, input: TileEntity, output: TileEntity, inputSide: number): void; function transportLiquid(liquid: string, maxAmount: number, output: TileEntity, input: TileEntity, outputSide: number): void; - function getContainerSlots(container: any, mode: number, side: number): (string | number)[]; + function getContainerSlots(container: NativeTileEntity | UI.Container | ItemContainer, mode?: number, side?: number): (string | number)[]; function checkHoppers(tile: TileEntity): void; function extractItems(items: ItemInstance[], containers: any, tile: TileEntity): void; } diff --git a/src/lib/StorageInterface.js b/src/lib/StorageInterface.js index 1ef13cc..cfba93b 100644 --- a/src/lib/StorageInterface.js +++ b/src/lib/StorageInterface.js @@ -1,6 +1,6 @@ LIBRARY({ name: "StorageInterface", - version: 7, + version: 8, shared: true, api: "CoreEngine" }); @@ -9,7 +9,7 @@ let LIQUID_STORAGE_MAX_LIMIT = 99999999; let StorageInterface = { data: {}, - + directionsBySide: [ {x: 0, y: -1, z: 0}, // down {x: 0, y: 1, z: 0}, // up @@ -18,12 +18,30 @@ let StorageInterface = { {x: -1, y: 0, z: 0}, // east {x: 1, y: 0, z: 0} // west ], - + getRelativeCoords: function(coords, side) { let dir = this.directionsBySide[side]; return {x: coords.x + dir.x, y: coords.y + dir.y, z: coords.z + dir.z}; }, - + + setSlotMaxStackPolicy: function(container, slotName, maxCount) { + container.setSlotAddTransferPolicy(slotName, function(container, name, id, amount, data) { + return Math.max(0, Math.min(amount, maxCount - container.getSlot(name).count)); + }); + }, + + setSlotValidatePolicy: function(container, slotName, func) { + container.setSlotAddTransferPolicy(slotName, function(container, name, id, amount, data, extra, playerUid) { + return func(name, id, amount, data, extra, container, playerUid) ? amount : 0; + }); + }, + + setGlobalValidatePolicy: function(container, func) { + container.setGlobalAddTransferPolicy(function(container, name, id, amount, data, extra, playerUid) { + return func(name, id, amount, data, extra, container, playerUid) ? amount : 0; + }); + }, + newInstance: function(id, tileEntity) { let instance = {}; let obj = this.data[id]; @@ -35,7 +53,7 @@ let StorageInterface = { instance.liquidStorage = tileEntity.liquidStorage; return instance; }, - + createInterface: function(id, interface) { tilePrototype = TileEntity.getPrototype(id); if (tilePrototype) { @@ -44,7 +62,7 @@ let StorageInterface = { this.interface = StorageInterface.newInstance(id, this); this._init(); } - + if (interface.slots) { for (let name in interface.slots) { if (name.includes('^')) { @@ -72,11 +90,11 @@ let StorageInterface = { else { interface.slots = {}; } - + tilePrototype.addTransportedItem = function(obj, item, side) { this.interface.addItem(item, side); } - + interface.isValidInput = interface.isValidInput || function(item, side, tileEntity) { return true; } @@ -95,7 +113,7 @@ let StorageInterface = { } return sideEnum[slotSideTag] == side; } - + interface.addItem = interface.addItem || function(item, side, maxCount) { if (!this.isValidInput(item)) return 0; let count = 0; @@ -109,7 +127,7 @@ let StorageInterface = { } return count; } - + interface.getOutputSlots = interface.getOutputSlots || function(side) { let slots = []; for (let name in this.slots) { @@ -123,7 +141,7 @@ let StorageInterface = { } return slots; } - + interface.canReceiveLiquid = interface.canReceiveLiquid || function(liquid, side) { return false; } @@ -143,10 +161,10 @@ let StorageInterface = { interface.getLiquidStored = interface.getLiquidStored || function(storage, side) { return this.liquidStorage.getLiquidStored(); } - - this.data[id] = interface; + + this.data[id] = interface; } else { - Logger.Log("failed to create storage interface: no tile entity for id "+id, "ERROR"); + Logger.Log("Failed to create storage interface: cannot find tile entity prototype for id "+id, "ERROR"); } }, @@ -170,12 +188,13 @@ let StorageInterface = { } return 0; }, - - getNearestContainers: function(coords, side, excludeSide) { + + getNearestContainers: function(coords, side, excludeSide, region) { + region = region || BlockSource.getDefaultForActor(Player.get()); let containers = {}; if (side >= 0 && !excludeSide) { let dir = this.getRelativeCoords(coords, side); - let container = World.getContainer(dir.x, dir.y, dir.z); + let container = World.getContainer(dir.x, dir.y, dir.z, region); if (container) { containers[side] = container; } @@ -183,19 +202,20 @@ let StorageInterface = { else for (let s = 0; s < 6; s++) { if (excludeSide && s == side) continue; let dir = this.getRelativeCoords(coords, s); - let container = World.getContainer(dir.x, dir.y, dir.z); + let container = World.getContainer(dir.x, dir.y, dir.z, region); if (container) { containers[s] = container; } } return containers; }, - - getNearestLiquidStorages: function(coords, side, excludeSide) { + + getNearestLiquidStorages: function(coords, side, excludeSide, region) { + region = region || BlockSource.getDefaultForActor(Player.get()); let storages = {}; if (side >= 0 && !excludeSide) { let dir = this.getRelativeCoords(coords, side); - let tileEntity = World.getTileEntity(dir.x, dir.y, dir.z); + let tileEntity = World.getTileEntity(dir.x, dir.y, dir.z, region); if (tileEntity && tileEntity.liquidStorage) { storages[side] = tileEntity; } @@ -203,14 +223,14 @@ let StorageInterface = { else for (let s = 0; s < 6; s++) { if (excludeSide && s == side) continue; let dir = this.getRelativeCoords(coords, s); - let tileEntity = World.getTileEntity(dir.x, dir.y, dir.z); + let tileEntity = World.getTileEntity(dir.x, dir.y, dir.z, region); if (tileEntity && tileEntity.liquidStorage) { storages[s] = tileEntity; } } return storages; }, - + putItems: function(items, containers) { for (let i in items) { let item = items[i]; @@ -221,14 +241,14 @@ let StorageInterface = { } } }, - + putItemToContainer: function(item, container, side, maxCount) { let tileEntity = container.tileEntity; let count = 0; let slots = []; let slotsInitialized = false; side ^= 1; // opposite side - + if (tileEntity) { if (tileEntity.interface) { return tileEntity.interface.addItem(item, side, maxCount); @@ -254,14 +274,14 @@ let StorageInterface = { } return count; }, - + extractItemsFromContainer: function(inputTile, container, side, maxCount, oneStack) { let outputTile = container.tileEntity; let count = 0; let slots = []; let slotsInitialized = false; let outputSide = side ^ 1; - + if (outputTile) { if (outputTile.interface) { slots = outputTile.interface.getOutputSlots(outputSide); @@ -290,7 +310,7 @@ let StorageInterface = { } return count; }, - + extractLiquid: function(liquid, maxAmount, input, output, inputSide) { if (!liquid) { if (output.interface) { @@ -306,7 +326,7 @@ let StorageInterface = { } } }, - + transportLiquid: function(liquid, maxAmount, output, input, outputSide) { if (liquid) { let inputSide = outputSide ^ 1; @@ -319,7 +339,7 @@ let StorageInterface = { } } }, - + getContainerSlots: function(container, mode, side) { let slots = []; if (container.slots) { @@ -361,11 +381,12 @@ let StorageInterface = { // require storage interface for tile entity checkHoppers: function(tile) { if (World.getThreadTime()%8 > 0) return; + let region = tile.blockSource; for (let side = 1; side < 6; side++) { let dir = this.getRelativeCoords(tile, side); - let block = World.getBlock(dir.x, dir.y, dir.z); + let block = region.getBlock(dir.x, dir.y, dir.z); if (block.id == 154 && block.data == side + Math.pow(-1, side)) { - let container = World.getContainer(dir.x, dir.y, dir.z); + let container = World.getContainer(dir.x, dir.y, dir.z, region); for (let s = 0; s < container.getSize(); s++) { let slot = container.getSlot(s); if (slot.id > 0 && tile.interface.addItem(slot, side, 1)) { @@ -376,8 +397,8 @@ let StorageInterface = { } } - if (World.getBlockID(tile.x, tile.y-1, tile.z) == 154) { - let container = World.getContainer(tile.x, tile.y-1, tile.z); + if (region.getBlockID(tile.x, tile.y-1, tile.z) == 154) { + let container = World.getContainer(tile.x, tile.y-1, tile.z, region); let slots = tile.interface.getOutputSlots(0); for (let i in slots) { let item = tile.container.getSlot(slots[i]); @@ -393,7 +414,7 @@ let StorageInterface = { } } }, - + // legacy extractItems: function(items, containers, tile) { for (let i in items) { @@ -405,7 +426,7 @@ let StorageInterface = { let slots = []; let slotsInitialized = false; let outputSide = parseInt(side) ^ 1; - + if (tileEntity) { if (tileEntity.interface) { slots = tileEntity.interface.getOutputSlots(outputSide); @@ -426,7 +447,7 @@ let StorageInterface = { if (tile.interface) { added = tile.interface.addItem(slot, parseInt(side)); } else { - added = this.addItemToSlot(slot, item) + added = this.addItemToSlot(slot, item); } if (added > 0 && !container.slots) { container.setSlot(slots[i], slot.id, slot.count, slot.data); From 585b3a07b060ce070b1ed31da148068f1e9c586e Mon Sep 17 00:00:00 2001 From: Nikolay Savenko Date: Mon, 30 Nov 2020 20:19:36 +0300 Subject: [PATCH 2/6] pipe render layer fixed --- src/dev/core/pipe/components/PipeBlock.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/dev/core/pipe/components/PipeBlock.ts b/src/dev/core/pipe/components/PipeBlock.ts index 355a52c..c00b5a2 100644 --- a/src/dev/core/pipe/components/PipeBlock.ts +++ b/src/dev/core/pipe/components/PipeBlock.ts @@ -1,8 +1,7 @@ const BlockTypePipe: Block.SpecialType = { base: 1, destroytime: 0.2, - explosionres: 0.5, - renderlayer: 1 + explosionres: 0.5 }; class PipeBlock { From 354a927234b9d7af69333806f9cfe6f953c8c025 Mon Sep 17 00:00:00 2001 From: Nikolay Savenko Date: Mon, 30 Nov 2020 20:43:27 +0300 Subject: [PATCH 3/6] fixed post define --- src/dev/core/engine/Engines.ts | 4 ---- .../core/engine/creative/CreativeEngine.ts | 3 ++- src/dev/core/engine/wood/WoodEngine.ts | 4 +++- src/dev/core/pipe/item/ItemPipes.ts | 21 ------------------- src/dev/core/pipe/item/cobble/PipeCobble.ts | 3 ++- src/dev/core/pipe/item/diamond/PipeDiamond.ts | 3 ++- src/dev/core/pipe/item/gold/PipeGold.ts | 3 ++- src/dev/core/pipe/item/iron/PipeIron.ts | 3 ++- .../core/pipe/item/obsidian/PipeObsidian.ts | 3 ++- src/dev/core/pipe/item/quartz/PipeQuartz.ts | 3 ++- .../core/pipe/item/sandstone/PipeSandstone.ts | 7 ++++--- src/dev/core/pipe/item/stone/PipeStone.ts | 3 ++- src/dev/core/pipe/item/void/PipeVoid.ts | 3 ++- src/dev/core/pipe/item/wooden/PipeWooden.ts | 3 ++- src/dev/tsconfig.json | 2 -- 15 files changed, 27 insertions(+), 41 deletions(-) delete mode 100644 src/dev/core/engine/Engines.ts delete mode 100644 src/dev/core/pipe/item/ItemPipes.ts diff --git a/src/dev/core/engine/Engines.ts b/src/dev/core/engine/Engines.ts deleted file mode 100644 index 280b5f4..0000000 --- a/src/dev/core/engine/Engines.ts +++ /dev/null @@ -1,4 +0,0 @@ -/// -/// -const creativeEngine = new CreativeEngine(); -const woodenEngine = new WoodEngine(); \ No newline at end of file diff --git a/src/dev/core/engine/creative/CreativeEngine.ts b/src/dev/core/engine/creative/CreativeEngine.ts index c438de4..549279e 100644 --- a/src/dev/core/engine/creative/CreativeEngine.ts +++ b/src/dev/core/engine/creative/CreativeEngine.ts @@ -24,4 +24,5 @@ class CreativeEngine extends BCEngine { protected getIngredientsForRecipe(): EngineIngredients { return null; } -} \ No newline at end of file +} +const creativeEngine = new CreativeEngine(); \ No newline at end of file diff --git a/src/dev/core/engine/wood/WoodEngine.ts b/src/dev/core/engine/wood/WoodEngine.ts index 7768863..79a470e 100644 --- a/src/dev/core/engine/wood/WoodEngine.ts +++ b/src/dev/core/engine/wood/WoodEngine.ts @@ -3,6 +3,7 @@ /// /// /// +/// class WoodEngine extends BCEngine { public get engineType(): string { @@ -20,4 +21,5 @@ class WoodEngine extends BCEngine { protected getIngredientsForRecipe(): EngineIngredients { return new EngineIngredients({ id: ItemID.gear_wood, count: 1, data: 0 }, { id: VanillaBlockID.planks, count: 1, data: -1 }); } -} \ No newline at end of file +} +const woodenEngine = new WoodEngine(); \ No newline at end of file diff --git a/src/dev/core/pipe/item/ItemPipes.ts b/src/dev/core/pipe/item/ItemPipes.ts deleted file mode 100644 index 529783c..0000000 --- a/src/dev/core/pipe/item/ItemPipes.ts +++ /dev/null @@ -1,21 +0,0 @@ -/// -/// -/// -/// -/// -/// -/// -/// -/// -/// - -const cobblePipe = new PipeCobble(); -const stonePipe = new PipeStone(); -const sandstonePipe = new PipeSandstone(); -const quartzPipe = new PipeQuartz(); -const goldPipe = new PipeGold(); -const woodPipe = new PipeWooden(); -const voidPipe = new PipeVoid(); -const ironPipe = new PipeIron(); -const diamondPipe = new PipeDiamond(); -const obsidianPipe = new PipeObsidian(); \ No newline at end of file diff --git a/src/dev/core/pipe/item/cobble/PipeCobble.ts b/src/dev/core/pipe/item/cobble/PipeCobble.ts index eb03ba9..f6721ef 100644 --- a/src/dev/core/pipe/item/cobble/PipeCobble.ts +++ b/src/dev/core/pipe/item/cobble/PipeCobble.ts @@ -26,4 +26,5 @@ class PipeCobble extends BCTransportPipe { protected getIngredientForRecipe(): ItemInstance { return { id: VanillaBlockID.cobblestone, count: 1, data: 0 } } -} \ No newline at end of file +} +const cobblePipe = new PipeCobble(); \ No newline at end of file diff --git a/src/dev/core/pipe/item/diamond/PipeDiamond.ts b/src/dev/core/pipe/item/diamond/PipeDiamond.ts index c415e7e..a499de5 100644 --- a/src/dev/core/pipe/item/diamond/PipeDiamond.ts +++ b/src/dev/core/pipe/item/diamond/PipeDiamond.ts @@ -34,4 +34,5 @@ class PipeDiamond extends BCTransportPipe { protected getIngredientForRecipe(): ItemInstance { return { id: VanillaItemID.diamond, count: 1, data: 0 } } -} \ No newline at end of file +} +const diamondPipe = new PipeDiamond(); \ No newline at end of file diff --git a/src/dev/core/pipe/item/gold/PipeGold.ts b/src/dev/core/pipe/item/gold/PipeGold.ts index 35811e6..dc92eee 100644 --- a/src/dev/core/pipe/item/gold/PipeGold.ts +++ b/src/dev/core/pipe/item/gold/PipeGold.ts @@ -18,4 +18,5 @@ class PipeGold extends BCTransportPipe { protected getIngredientForRecipe(): ItemInstance { return { id: VanillaItemID.gold_ingot, count: 1, data: 0 } } -} \ No newline at end of file +} +const goldPipe = new PipeGold(); \ No newline at end of file diff --git a/src/dev/core/pipe/item/iron/PipeIron.ts b/src/dev/core/pipe/item/iron/PipeIron.ts index abbde2e..732fa2f 100644 --- a/src/dev/core/pipe/item/iron/PipeIron.ts +++ b/src/dev/core/pipe/item/iron/PipeIron.ts @@ -34,4 +34,5 @@ class PipeIron extends BCTransportPipe { protected getIngredientForRecipe(): ItemInstance { return { id: VanillaItemID.iron_ingot, count: 1, data: 0 } } -} \ No newline at end of file +} +const ironPipe = new PipeIron(); \ No newline at end of file diff --git a/src/dev/core/pipe/item/obsidian/PipeObsidian.ts b/src/dev/core/pipe/item/obsidian/PipeObsidian.ts index 36a9fab..a759ba0 100644 --- a/src/dev/core/pipe/item/obsidian/PipeObsidian.ts +++ b/src/dev/core/pipe/item/obsidian/PipeObsidian.ts @@ -42,4 +42,5 @@ class PipeObsidian extends BCTransportPipe { protected getIngredientForRecipe(): ItemInstance { return { id: VanillaBlockID.obsidian, count: 1, data: 0 } } -} \ No newline at end of file +} +const obsidianPipe = new PipeObsidian(); \ No newline at end of file diff --git a/src/dev/core/pipe/item/quartz/PipeQuartz.ts b/src/dev/core/pipe/item/quartz/PipeQuartz.ts index 32e1632..4dd26b0 100644 --- a/src/dev/core/pipe/item/quartz/PipeQuartz.ts +++ b/src/dev/core/pipe/item/quartz/PipeQuartz.ts @@ -31,4 +31,5 @@ class PipeQuartz extends BCTransportPipe { protected getIngredientForRecipe(): ItemInstance { return { id: VanillaItemID.quartz, count: 1, data: 0 } } -} \ No newline at end of file +} +const quartzPipe = new PipeQuartz(); \ No newline at end of file diff --git a/src/dev/core/pipe/item/sandstone/PipeSandstone.ts b/src/dev/core/pipe/item/sandstone/PipeSandstone.ts index 77c3cdc..3f8d4e5 100644 --- a/src/dev/core/pipe/item/sandstone/PipeSandstone.ts +++ b/src/dev/core/pipe/item/sandstone/PipeSandstone.ts @@ -6,17 +6,18 @@ class PipeSandstone extends BCTransportPipe { } public get pipeConnector(): PipeConnector { - if(!this.connector) this.connector = new SandstonePipeConnector(); + if (!this.connector) this.connector = new SandstonePipeConnector(); return this.connector; } protected get pipeTexture(): PipeTexture { const textureName = `pipe_${this.transportType}_${this.material}` - if(!this.texture) this.texture = new PipeTexture({name: textureName, data: 0}, {name: textureName, data: 1}); + if (!this.texture) this.texture = new PipeTexture({ name: textureName, data: 0 }, { name: textureName, data: 1 }); return this.texture; } protected getIngredientForRecipe(): ItemInstance { return { id: VanillaBlockID.sandstone, count: 1, data: 0 } } -} \ No newline at end of file +} +const sandstonePipe = new PipeSandstone(); \ No newline at end of file diff --git a/src/dev/core/pipe/item/stone/PipeStone.ts b/src/dev/core/pipe/item/stone/PipeStone.ts index dfe0444..bc299c1 100644 --- a/src/dev/core/pipe/item/stone/PipeStone.ts +++ b/src/dev/core/pipe/item/stone/PipeStone.ts @@ -26,4 +26,5 @@ class PipeStone extends BCTransportPipe { protected getIngredientForRecipe(): ItemInstance { return { id: VanillaBlockID.stone, count: 1, data: 0 } } -} \ No newline at end of file +} +const stonePipe = new PipeStone(); \ No newline at end of file diff --git a/src/dev/core/pipe/item/void/PipeVoid.ts b/src/dev/core/pipe/item/void/PipeVoid.ts index 80ddcbe..71b652b 100644 --- a/src/dev/core/pipe/item/void/PipeVoid.ts +++ b/src/dev/core/pipe/item/void/PipeVoid.ts @@ -35,4 +35,5 @@ class PipeVoid extends BCTransportPipe { protected getIngredientForRecipe(): ItemInstance { return null } -} \ No newline at end of file +} +const voidPipe = new PipeVoid(); \ No newline at end of file diff --git a/src/dev/core/pipe/item/wooden/PipeWooden.ts b/src/dev/core/pipe/item/wooden/PipeWooden.ts index 9595583..0ea9ca8 100644 --- a/src/dev/core/pipe/item/wooden/PipeWooden.ts +++ b/src/dev/core/pipe/item/wooden/PipeWooden.ts @@ -54,4 +54,5 @@ class PipeWooden extends BCTransportPipe { protected getIngredientForRecipe(): ItemInstance { return { id: VanillaBlockID.planks, count: 1, data: 0 } } -} \ No newline at end of file +} +const woodPipe = new PipeWooden(); \ No newline at end of file diff --git a/src/dev/tsconfig.json b/src/dev/tsconfig.json index b9f05f6..10a4815 100644 --- a/src/dev/tsconfig.json +++ b/src/dev/tsconfig.json @@ -29,7 +29,6 @@ "core/engine/components", "core/engine/creative", "core/engine/EngineHeat.ts", - "core/engine/Engines.ts", "core/engine/EngineTextures.ts", "core/engine/interface", "core/engine/model", @@ -91,7 +90,6 @@ "core/pipe/item/gold", "core/pipe/item/iron", "core/pipe/item/ItemMachines.ts", - "core/pipe/item/ItemPipes.ts", "core/pipe/item/obsidian", "core/pipe/item/quartz", "core/pipe/item/sandstone", From 736db93903d0d16511377a32fa32d13d766ee5f3 Mon Sep 17 00:00:00 2001 From: Nikolay Savenko Date: Wed, 9 Dec 2020 23:47:10 +0300 Subject: [PATCH 4/6] updated StorageInterface --- declarations/lib/StorageInterface.d.ts | 84 +- .../item/travelingItem/TravelingItemMover.ts | 2 +- .../pipe/item/wooden/WoodenPipeItemEjector.ts | 7 +- src/lib/StorageInterface.js | 900 +++++++++--------- 4 files changed, 514 insertions(+), 479 deletions(-) diff --git a/declarations/lib/StorageInterface.d.ts b/declarations/lib/StorageInterface.d.ts index b31d00d..429fd0d 100644 --- a/declarations/lib/StorageInterface.d.ts +++ b/declarations/lib/StorageInterface.d.ts @@ -1,21 +1,83 @@ -/// - +interface StorageDescriptor { + slots?: { + [key: string]: SlotInterface; + }; + isValidInput?(item: ItemInstance, side: number, tileEntity: TileEntity): boolean; + addItem?(item: ItemInstance, side: number, maxCount: number): number; + getOutputSlots?(side: number): string[] | number[]; + canReceiveLiquid?(liquid: string, side?: number): boolean; + canTransportLiquid?(liquid: string, side?: number): boolean; + addLiquid?(liquid: string, amount: number): number; + getLiquid?(liquid: string, amount: number): number; + getLiquidStored?(storage: string, side: number): string; +} +interface IStorage extends StorageDescriptor { + isNativeContainer(): boolean; + getSlot(name: string | number): ItemInstance; + setSlot(name: string | number, id: number, count: number, data: number, extra?: ItemExtraData): void; +} +interface SlotInterface { + input?: boolean; + output?: boolean; + side?: number | "horizontal" | "down" | "up"; + isValid?(item: ItemInstance, side: number, tileEntity: TileEntity): boolean; + canOutput?(item: ItemInstance, side: number, tileEntity: TileEntity): boolean; +} +declare class NativeContainerInterface implements IStorage { + container: NativeTileEntity; + constructor(container: NativeTileEntity); + isNativeContainer(): boolean; + getSlot(index: number): ItemInstance; + setSlot(index: number, id: number, count: number, data: number, extra?: ItemExtraData): void; + private isValidInputSlot; + addItem(item: ItemInstance, side: number, maxCount: number): number; + getOutputSlots(side: number): number[]; +} +declare class TileEntityInterface implements IStorage { + slots?: { + [key: string]: SlotInterface; + }; + container: UI.Container | ItemContainer; + tileEntity: TileEntity; + liquidStorage: any; + constructor(tileEntity: TileEntity); + isNativeContainer(): boolean; + getSlot(name: string): ItemInstance; + setSlot(name: string, id: number, count: number, data: number, extra?: ItemExtraData): void; + isValidInput(item: ItemInstance, side: number, tileEntity: TileEntity): boolean; + checkSide(slotSideTag: string | number, side: number): boolean; + addItem(item: ItemInstance, side: number, maxCount?: number): number; + getOutputSlots(side: number): string[]; + canReceiveLiquid(liquid: string, side?: number): boolean; + canTransportLiquid(liquid: string, side?: number): boolean; + addLiquid(liquid: string, amount: number): number; + getLiquid(liquid: string, amount: number): number; + getLiquidStored(storage?: string, side?: number): string; +} +declare let LIQUID_STORAGE_MAX_LIMIT: number; +declare type Container = NativeTileEntity | UI.Container | ItemContainer; declare namespace StorageInterface { - const directionsBySide: Vector[]; + var data: {}; + var directionsBySide: { + x: number; + y: number; + z: number; + }[]; function getRelativeCoords(coords: Vector, side: number): Vector; function setSlotMaxStackPolicy(container: ItemContainer, slotName: string, maxCount: number): void; function setSlotValidatePolicy(container: ItemContainer, slotName: string, func: (name: string, id: number, amount: number, data: number, extra: ItemExtraData, container: ItemContainer, playerUid: number) => boolean): void; function setGlobalValidatePolicy(container: ItemContainer, func: (name: string, id: number, amount: number, data: number, extra: ItemExtraData, container: ItemContainer, playerUid: number) => boolean): void; - function createInterface(id: number, interface: any): void; + function newInstance(storage: TileEntity | Container): IStorage; + function createInterface(id: number, interface: StorageDescriptor): void; function addItemToSlot(item: ItemInstance, slot: ItemInstance, count: number): number; - function getNearestContainers(coords: Vector, side: number, excludeSide?: boolean, region?: BlockSource): {}; - function getNearestLiquidStorages(coords: Vector, side: number, excludeSide?: boolean, region?: BlockSource): {}; - function putItems(items: ItemInstance[], containers: any): void; - function putItemToContainer(item: ItemInstance, container: NativeTileEntity | UI.Container | ItemContainer, side: number, maxCount?: number): number; - function extractItemsFromContainer(inputTile: TileEntity, container: NativeTileEntity | UI.Container | ItemContainer, side: number, maxCount?: number, oneStack?: boolean): number; + function getStorage(region: BlockSource, x: number, y: number, z: number): IStorage; + function getNearestContainers(coords: Vector, side: number, excludeSide?: boolean): object; + function getNearestLiquidStorages(coords: Vector, side: number, excludeSide?: boolean): object; + function getContainerSlots(container: Container): string[] | number[]; + function putItems(items: ItemInstance[], containers: object): void; + function putItemToContainer(item: ItemInstance, container: Container, side: number, maxCount?: number): number; + function extractItemsFromContainer(inputContainer: TileEntity | Container, outputContainer: Container, side: number, maxCount?: number, oneStack?: boolean): number; function extractLiquid(liquid: string, maxAmount: number, input: TileEntity, output: TileEntity, inputSide: number): void; function transportLiquid(liquid: string, maxAmount: number, output: TileEntity, input: TileEntity, outputSide: number): void; - function getContainerSlots(container: NativeTileEntity | UI.Container | ItemContainer, mode?: number, side?: number): (string | number)[]; function checkHoppers(tile: TileEntity): void; - function extractItems(items: ItemInstance[], containers: any, tile: TileEntity): void; } diff --git a/src/dev/core/pipe/item/travelingItem/TravelingItemMover.ts b/src/dev/core/pipe/item/travelingItem/TravelingItemMover.ts index 00701b4..ced9058 100644 --- a/src/dev/core/pipe/item/travelingItem/TravelingItemMover.ts +++ b/src/dev/core/pipe/item/travelingItem/TravelingItemMover.ts @@ -179,7 +179,7 @@ class TravelingItemMover { } public isValidContainer(container): boolean { - const slots = StorageInterface.getContainerSlots(container, 1, 0); + const slots = StorageInterface.getContainerSlots(container); let trueSlotsLength = slots.length; if (trueSlotsLength > 0 && typeof slots[0] == "string") { // ! tileEntity container has jsonSaverId in slots[0] diff --git a/src/dev/core/pipe/item/wooden/WoodenPipeItemEjector.ts b/src/dev/core/pipe/item/wooden/WoodenPipeItemEjector.ts index ada2a91..dbdced7 100644 --- a/src/dev/core/pipe/item/wooden/WoodenPipeItemEjector.ts +++ b/src/dev/core/pipe/item/wooden/WoodenPipeItemEjector.ts @@ -7,7 +7,7 @@ class WoodenPipeItemEjector { public readonly x: number, public readonly y: number, public readonly z: number - ) {} + ) { } public set connectionSide(value: number | null) { this.side = value; @@ -15,10 +15,9 @@ class WoodenPipeItemEjector { const coords = World.getRelativeCoords(this.x, this.y, this.z, this.connectionSide); // update to BlockSource const sourceContainer = World.getContainer(coords.x, coords.y, coords.z, this.region); - const containerSide = World.getInverseBlockSide(value); this.containerData = { source: sourceContainer, - slots: StorageInterface.getContainerSlots(sourceContainer, 1, containerSide), + slots: StorageInterface.getContainerSlots(sourceContainer), }; } else { this.containerData = null; @@ -95,7 +94,7 @@ class WoodenPipeItemEjector { const needToAdd = count - gettedItem.count; if (needToAdd > 0) { - // ! MineExplorer, update library please + // * MineExplorer thanks for StorageInterface StorageInterface.addItemToSlot(slot, gettedItem, needToAdd); source.setSlot(i, slot.id, slot.count, slot.data, slot.extra); } else break; diff --git a/src/lib/StorageInterface.js b/src/lib/StorageInterface.js index cfba93b..95e6f10 100644 --- a/src/lib/StorageInterface.js +++ b/src/lib/StorageInterface.js @@ -1,465 +1,439 @@ +var NativeContainerInterface = /** @class */ (function () { + function NativeContainerInterface(container) { + this.container = container; + } + NativeContainerInterface.prototype.isNativeContainer = function () { + return true; + }; + NativeContainerInterface.prototype.getSlot = function (index) { + return this.container.getSlot(index); + }; + NativeContainerInterface.prototype.setSlot = function (index, id, count, data, extra) { + this.container.setSlot(index, id, count, data, extra); + }; + NativeContainerInterface.prototype.isValidInputSlot = function (containerType, index, side) { + switch (containerType) { + case 1: + case 38: + case 39: + return index == ((side == 1) ? 0 : 1); + case 8: + return index == ((side == 1) ? 0 : 4); + default: + return true; + } + }; + NativeContainerInterface.prototype.addItem = function (item, side, maxCount) { + var count = 0; + var containerType = this.container.getType(); + var containerSize = this.container.getSize(); + for (var index = 0; index < containerSize; index++) { + if (!this.isValidInputSlot(containerType, index, side)) + continue; + var slot = this.getSlot(index); + var added = StorageInterface.addItemToSlot(item, slot, maxCount - count); + if (added > 0) { + count += added; + this.setSlot(index, slot.id, slot.count, slot.data, slot.extra); + if (item.count == 0 || count >= maxCount) { + break; + } + } + } + return count; + }; + NativeContainerInterface.prototype.getOutputSlots = function (side) { + var slots = []; + var type = this.container.getType(); + switch (type) { + case 1: + case 38: + case 39: + slots.push(2); + break; + case 8: + slots.push(1, 2, 3); + break; + default: + for (var i = 0; i < this.container.getSize(); i++) { + slots.push(i); + } + break; + } + return slots; + }; + return NativeContainerInterface; +}()); +var TileEntityInterface = /** @class */ (function () { + function TileEntityInterface(tileEntity) { + this.tileEntity = tileEntity; + this.container = tileEntity.container; + this.liquidStorage = tileEntity.liquidStorage; + var storagePrototype = StorageInterface.data[tileEntity.blockID]; + if (storagePrototype) { + for (var key in storagePrototype) { + this[key] = storagePrototype[key]; + } + } + } + TileEntityInterface.prototype.isNativeContainer = function () { + return false; + }; + TileEntityInterface.prototype.getSlot = function (name) { + return this.container.getSlot(name); + }; + TileEntityInterface.prototype.setSlot = function (name, id, count, data, extra) { + this.container.setSlot(name, id, count, data, extra); + }; + TileEntityInterface.prototype.isValidInput = function (item, side, tileEntity) { + return true; + }; + TileEntityInterface.prototype.checkSide = function (slotSideTag, side) { + if (slotSideTag == undefined) + return true; + if (typeof slotSideTag == "number") + return slotSideTag == side; + return (slotSideTag == "horizontal" && side > 1) || (slotSideTag == "down" && side == 0) || (slotSideTag == "up" && side == 1); + }; + TileEntityInterface.prototype.addItem = function (item, side, maxCount) { + if (maxCount === void 0) { maxCount = 64; } + if (!this.isValidInput(item, side, this.tileEntity)) + return 0; + var count = 0; + for (var name in this.slots) { + var slotData = this.slots[name]; + if (slotData.input && this.checkSide(slotData.side, side) && (!slotData.isValid || slotData.isValid(item, side, this.tileEntity))) { + var slot = this.container.getSlot(name); + count += StorageInterface.addItemToSlot(item, slot, maxCount - count); + if (item.count == 0 || count >= maxCount) { + break; + } + } + } + return count; + }; + TileEntityInterface.prototype.getOutputSlots = function (side) { + var slotNames = []; + if (this.slots) { + for (var name in this.slots) { + var slotData = this.slots[name]; + if (slotData.output) { + var item = this.container.getSlot(name); + if (item.id > 0 && (!slotData.side || slotData.side == side) && (!slotData.canOutput || slotData.canOutput(item, side, this.tileEntity))) { + slotNames.push(name); + } + } + } + } + else if (this.tileEntity.getTransportSlots) { + return this.tileEntity.getTransportSlots().output; + } + else { + for (var name in this.container.slots) { + slotNames.push(name); + } + } + return slotNames; + }; + TileEntityInterface.prototype.canReceiveLiquid = function (liquid, side) { + return this.liquidStorage.getLimit(liquid) < LIQUID_STORAGE_MAX_LIMIT; + }; + TileEntityInterface.prototype.canTransportLiquid = function (liquid, side) { + return this.liquidStorage.getLimit(liquid) < LIQUID_STORAGE_MAX_LIMIT; + }; + TileEntityInterface.prototype.addLiquid = function (liquid, amount) { + var liquidStored = this.liquidStorage.getLiquidStored(); + if (!liquidStored || liquidStored == liquid) { + return this.liquidStorage.addLiquid(liquid, amount); + } + return amount; + }; + TileEntityInterface.prototype.getLiquid = function (liquid, amount) { + return this.liquidStorage.getLiquid(liquid, amount); + }; + TileEntityInterface.prototype.getLiquidStored = function (storage, side) { + return this.liquidStorage.getLiquidStored(); + }; + return TileEntityInterface; +}()); LIBRARY({ - name: "StorageInterface", - version: 8, - shared: true, - api: "CoreEngine" + name: "StorageInterface", + version: 8, + shared: true, + api: "CoreEngine" }); - -let LIQUID_STORAGE_MAX_LIMIT = 99999999; - -let StorageInterface = { - data: {}, - - directionsBySide: [ - {x: 0, y: -1, z: 0}, // down - {x: 0, y: 1, z: 0}, // up - {x: 0, y: 0, z: -1}, // north - {x: 0, y: 0, z: 1}, // south - {x: -1, y: 0, z: 0}, // east - {x: 1, y: 0, z: 0} // west - ], - - getRelativeCoords: function(coords, side) { - let dir = this.directionsBySide[side]; - return {x: coords.x + dir.x, y: coords.y + dir.y, z: coords.z + dir.z}; - }, - - setSlotMaxStackPolicy: function(container, slotName, maxCount) { - container.setSlotAddTransferPolicy(slotName, function(container, name, id, amount, data) { - return Math.max(0, Math.min(amount, maxCount - container.getSlot(name).count)); - }); - }, - - setSlotValidatePolicy: function(container, slotName, func) { - container.setSlotAddTransferPolicy(slotName, function(container, name, id, amount, data, extra, playerUid) { - return func(name, id, amount, data, extra, container, playerUid) ? amount : 0; - }); - }, - - setGlobalValidatePolicy: function(container, func) { - container.setGlobalAddTransferPolicy(function(container, name, id, amount, data, extra, playerUid) { - return func(name, id, amount, data, extra, container, playerUid) ? amount : 0; - }); - }, - - newInstance: function(id, tileEntity) { - let instance = {}; - let obj = this.data[id]; - for (let i in obj) { - instance[i] = obj[i]; - } - instance.tileEntity = tileEntity; - instance.container = tileEntity.container; - instance.liquidStorage = tileEntity.liquidStorage; - return instance; - }, - - createInterface: function(id, interface) { - tilePrototype = TileEntity.getPrototype(id); - if (tilePrototype) { - tilePrototype._init = tilePrototype.init; - tilePrototype.init = function() { - this.interface = StorageInterface.newInstance(id, this); - this._init(); - } - - if (interface.slots) { - for (let name in interface.slots) { - if (name.includes('^')) { - let slotData = interface.slots[name]; - let str = name.split('^'); - let index = str[1].split('-'); - for (let i = parseInt(index[0]); i <= parseInt(index[1]); i++) { - interface.slots[str[0] + i] = slotData; - } - delete interface.slots[name]; - } - } - if (!tilePrototype.getTransportSlots) { - let inputSlots = [], outputSlots = []; - for (let i in interface.slots) { - let slot = interface.slots[i]; - if (slot.input) inputSlots.push(i); - if (slot.output) outputSlots.push(i); - } - tilePrototype.getTransportSlots = function() { - return {input: inputSlots, output: outputSlots}; - } - } - } - else { - interface.slots = {}; - } - - tilePrototype.addTransportedItem = function(obj, item, side) { - this.interface.addItem(item, side); - } - - interface.isValidInput = interface.isValidInput || function(item, side, tileEntity) { - return true; - } - - interface.checkSide = function(slotSideTag, side) { - if (slotSideTag == undefined) return true; - if (typeof slotSideTag == "number") return slotSideTag == side; - if (slotSideTag == "horizontal" && side > 1) return true; - let sideEnum = { - down: 0, - up: 1, - north: 2, - south: 3, - east: 4, - west: 5 - } - return sideEnum[slotSideTag] == side; - } - - interface.addItem = interface.addItem || function(item, side, maxCount) { - if (!this.isValidInput(item)) return 0; - let count = 0; - for (let name in this.slots) { - let slotData = this.slots[name]; - if (slotData.input && this.checkSide(slotData.side, side) && (!slotData.isValid || slotData.isValid(item, side, this.tileEntity))) { - let slot = this.container.getSlot(name); - count += StorageInterface.addItemToSlot(item, slot, maxCount - count); - if (item.count == 0 || count >= maxCount) {break;} - } - } - return count; - } - - interface.getOutputSlots = interface.getOutputSlots || function(side) { - let slots = []; - for (let name in this.slots) { - let slotData = this.slots[name]; - if (slotData.output) { - let item = this.container.getSlot(name); - if (item.id > 0 && (!slotData.side || slotData.side == side) && (!slotData.canOutput || slotData.canOutput(item, side, this.tileEntity))) { - slots.push(name); - } - } - } - return slots; - } - - interface.canReceiveLiquid = interface.canReceiveLiquid || function(liquid, side) { - return false; - } - interface.canTransportLiquid = interface.canTransportLiquid || function(liquid, side) { - return false; - } - interface.addLiquid = interface.addLiquid || function(liquid, amount) { - let liquidStored = this.liquidStorage.getLiquidStored(); - if (!liquidStored || liquidStored == liquid) { - return this.liquidStorage.addLiquid(liquid, amount); - } - return amount; - } - interface.getLiquid = interface.getLiquid || function(liquid, amount) { - return this.liquidStorage.getLiquid(liquid, amount); - } - interface.getLiquidStored = interface.getLiquidStored || function(storage, side) { - return this.liquidStorage.getLiquidStored(); - } - - this.data[id] = interface; - } else { - Logger.Log("Failed to create storage interface: cannot find tile entity prototype for id "+id, "ERROR"); - } - }, - - // doesn't override native container slot (only slot object) - addItemToSlot: function(item, slot, count) { - if (slot.id == 0 || slot.id == item.id && slot.data == item.data) { - let maxStack = Item.getMaxStack(item.id); - let add = Math.min(maxStack - slot.count, item.count); - if (count) add = Math.min(add, count); - if (add > 0) { - slot.id = item.id; - slot.count += add; - slot.data = item.data; - if (item.extra) slot.extra = item.extra; - item.count -= add; - if (item.count == 0) { - item.id = item.data = 0; - } - return add; - } - } - return 0; - }, - - getNearestContainers: function(coords, side, excludeSide, region) { - region = region || BlockSource.getDefaultForActor(Player.get()); - let containers = {}; - if (side >= 0 && !excludeSide) { - let dir = this.getRelativeCoords(coords, side); - let container = World.getContainer(dir.x, dir.y, dir.z, region); - if (container) { - containers[side] = container; - } - } - else for (let s = 0; s < 6; s++) { - if (excludeSide && s == side) continue; - let dir = this.getRelativeCoords(coords, s); - let container = World.getContainer(dir.x, dir.y, dir.z, region); - if (container) { - containers[s] = container; - } - } - return containers; - }, - - getNearestLiquidStorages: function(coords, side, excludeSide, region) { - region = region || BlockSource.getDefaultForActor(Player.get()); - let storages = {}; - if (side >= 0 && !excludeSide) { - let dir = this.getRelativeCoords(coords, side); - let tileEntity = World.getTileEntity(dir.x, dir.y, dir.z, region); - if (tileEntity && tileEntity.liquidStorage) { - storages[side] = tileEntity; - } - } - else for (let s = 0; s < 6; s++) { - if (excludeSide && s == side) continue; - let dir = this.getRelativeCoords(coords, s); - let tileEntity = World.getTileEntity(dir.x, dir.y, dir.z, region); - if (tileEntity && tileEntity.liquidStorage) { - storages[s] = tileEntity; - } - } - return storages; - }, - - putItems: function(items, containers) { - for (let i in items) { - let item = items[i]; - for (let side in containers) { - if (item.count == 0) break; - let container = containers[side]; - this.putItemToContainer(item, container, parseInt(side)); - } - } - }, - - putItemToContainer: function(item, container, side, maxCount) { - let tileEntity = container.tileEntity; - let count = 0; - let slots = []; - let slotsInitialized = false; - side ^= 1; // opposite side - - if (tileEntity) { - if (tileEntity.interface) { - return tileEntity.interface.addItem(item, side, maxCount); - } - if (tileEntity.getTransportSlots) { - slots = tileEntity.getTransportSlots().input || []; - slotsInitialized = true; - } - } - if (!slotsInitialized) { - slots = this.getContainerSlots(container, 1, side); - } - for (let i in slots) { - let slot = container.getSlot(slots[i]); - let added = this.addItemToSlot(item, slot, maxCount - count) - if (added > 0) { - count += added; - if (!container.slots) { - container.setSlot(i, slot.id, slot.count, slot.data); - } - if (item.count == 0 || count >= maxCount) {break;} - } - } - return count; - }, - - extractItemsFromContainer: function(inputTile, container, side, maxCount, oneStack) { - let outputTile = container.tileEntity; - let count = 0; - let slots = []; - let slotsInitialized = false; - let outputSide = side ^ 1; - - if (outputTile) { - if (outputTile.interface) { - slots = outputTile.interface.getOutputSlots(outputSide); - slotsInitialized = true; - } - else if (outputTile.getTransportSlots) { - slots = outputTile.getTransportSlots().output || []; - slotsInitialized = true; - } - } - if (!slotsInitialized) { - slots = this.getContainerSlots(container, 2, outputSide); - } - for (let i in slots) { - let slot = container.getSlot(slots[i]); - if (slot.id > 0) { - let added = inputTile.interface.addItem(slot, side, maxCount - count); - if (added > 0) { - count += added; - if (!container.slots) { - container.setSlot(slots[i], slot.id, slot.count, slot.data); - } - if (oneStack || count >= maxCount) {break;} - } - } - } - return count; - }, - - extractLiquid: function(liquid, maxAmount, input, output, inputSide) { - if (!liquid) { - if (output.interface) { - liquid = output.interface.getLiquidStored("output"); - } else { - liquid = output.liquidStorage.getLiquidStored(); - } - } - if (liquid) { - let outputSide = inputSide ^ 1; - if (!output.interface || output.interface.canTransportLiquid(liquid, outputSide)) { - this.transportLiquid(liquid, maxAmount, output, input, outputSide); - } - } - }, - - transportLiquid: function(liquid, maxAmount, output, input, outputSide) { - if (liquid) { - let inputSide = outputSide ^ 1; - let inputStorage = input.interface || input.liquidStorage; - let outputStorage = output.interface || output.liquidStorage; - if (!input.interface && inputStorage.getLimit(liquid) < LIQUID_STORAGE_MAX_LIMIT || input.interface && input.interface.canReceiveLiquid(liquid, inputSide)) { - let amount = outputStorage.getLiquid(liquid, maxAmount); - amount = inputStorage.addLiquid(liquid, amount); - outputStorage.getLiquid(liquid, -amount); - } - } - }, - - getContainerSlots: function(container, mode, side) { - let slots = []; - if (container.slots) { - for (let name in container.slots) { - slots.push(name); - } - } else { - let type = mode? container.getType() : 0; - switch(type) { - case 1: - case 38: - case 39: - if (mode == 1) { - slots.push((side == 1)? 0 : 1); - } - if (mode == 2) { - slots.push(2); - } - break; - case 8: - if (mode == 1) { - slots.push((side == 1)? 0 : 4); - } - if (mode == 2) { - slots.push(1, 2, 3); - } - break; - default: - for (let i = 0; i < container.getSize(); i++) { - slots.push(i); - } - break; - } - } - return slots; - }, - - // use it in tick function of tile entity - // require storage interface for tile entity - checkHoppers: function(tile) { - if (World.getThreadTime()%8 > 0) return; - let region = tile.blockSource; - for (let side = 1; side < 6; side++) { - let dir = this.getRelativeCoords(tile, side); - let block = region.getBlock(dir.x, dir.y, dir.z); - if (block.id == 154 && block.data == side + Math.pow(-1, side)) { - let container = World.getContainer(dir.x, dir.y, dir.z, region); - for (let s = 0; s < container.getSize(); s++) { - let slot = container.getSlot(s); - if (slot.id > 0 && tile.interface.addItem(slot, side, 1)) { - container.setSlot(s, slot.id, slot.count, slot.data); - break; - } - } - } - } - - if (region.getBlockID(tile.x, tile.y-1, tile.z) == 154) { - let container = World.getContainer(tile.x, tile.y-1, tile.z, region); - let slots = tile.interface.getOutputSlots(0); - for (let i in slots) { - let item = tile.container.getSlot(slots[i]); - if (item.id > 0) { - for (let s = 0; s < container.getSize(); s++) { - let slot = container.getSlot(s); - if (this.addItemToSlot(item, slot, 1)) { - container.setSlot(s, slot.id, slot.count, slot.data); - return; - } - } - } - } - } - }, - - // legacy - extractItems: function(items, containers, tile) { - for (let i in items) { - let item = items[i]; - let maxStack = Item.getMaxStack(item.id); - for (let side in containers) { - let container = containers[side]; - let tileEntity = container.tileEntity; - let slots = []; - let slotsInitialized = false; - let outputSide = parseInt(side) ^ 1; - - if (tileEntity) { - if (tileEntity.interface) { - slots = tileEntity.interface.getOutputSlots(outputSide); - slotsInitialized = true; - } - else if (tileEntity.getTransportSlots) { - slots = tileEntity.getTransportSlots().output || []; - slotsInitialized = true; - } - } - if (!slotsInitialized) { - slots = this.getContainerSlots(container, 2, outputSide); - } - for (let i in slots) { - let slot = container.getSlot(slots[i]); - if (slot.id > 0) { - let added = 0; - if (tile.interface) { - added = tile.interface.addItem(slot, parseInt(side)); - } else { - added = this.addItemToSlot(slot, item); - } - if (added > 0 && !container.slots) { - container.setSlot(slots[i], slot.id, slot.count, slot.data); - } - if (item.count == maxStack) {break;} - } - } - if (item.count == maxStack) {break;} - } - if (tile.interface) {return;} - } - } -} - -EXPORT("StorageInterface", StorageInterface); \ No newline at end of file +var LIQUID_STORAGE_MAX_LIMIT = 99999999; +var StorageInterface; +(function (StorageInterface) { + StorageInterface.data = {}; + StorageInterface.directionsBySide = [ + { x: 0, y: -1, z: 0 }, + { x: 0, y: 1, z: 0 }, + { x: 0, y: 0, z: -1 }, + { x: 0, y: 0, z: 1 }, + { x: -1, y: 0, z: 0 }, + { x: 1, y: 0, z: 0 } // west + ]; + function getRelativeCoords(coords, side) { + var dir = StorageInterface.directionsBySide[side]; + return { x: coords.x + dir.x, y: coords.y + dir.y, z: coords.z + dir.z }; + } + StorageInterface.getRelativeCoords = getRelativeCoords; + function setSlotMaxStackPolicy(container, slotName, maxCount) { + container.setSlotAddTransferPolicy(slotName, function (container, name, id, amount, data) { + return Math.max(0, Math.min(amount, maxCount - container.getSlot(name).count)); + }); + } + StorageInterface.setSlotMaxStackPolicy = setSlotMaxStackPolicy; + function setSlotValidatePolicy(container, slotName, func) { + container.setSlotAddTransferPolicy(slotName, function (container, name, id, amount, data, extra, playerUid) { + return func(name, id, amount, data, extra, container, playerUid) ? amount : 0; + }); + } + StorageInterface.setSlotValidatePolicy = setSlotValidatePolicy; + function setGlobalValidatePolicy(container, func) { + container.setGlobalAddTransferPolicy(function (container, name, id, amount, data, extra, playerUid) { + return func(name, id, amount, data, extra, container, playerUid) ? amount : 0; + }); + } + StorageInterface.setGlobalValidatePolicy = setGlobalValidatePolicy; + function newInstance(storage) { + if ("container" in storage) { + return new TileEntityInterface(storage); + } + if ("getParent" in storage) { + return new TileEntityInterface(storage.getParent()); + } + return new NativeContainerInterface(storage); + } + StorageInterface.newInstance = newInstance; + function createInterface(id, interface) { + var tilePrototype = TileEntity.getPrototype(id); + if (tilePrototype) { + if (interface.slots) { + for (var name in interface.slots) { + if (name.includes('^')) { + var slotData = interface.slots[name]; + var str = name.split('^'); + var index = str[1].split('-'); + for (var i = parseInt(index[0]); i <= parseInt(index[1]); i++) { + interface.slots[str[0] + i] = slotData; + } + delete interface.slots[name]; + } + } + if (!tilePrototype.getTransportSlots) { + var inputSlots_1 = [], outputSlots_1 = []; + for (var i in interface.slots) { + var slot = interface.slots[i]; + if (slot.input) + inputSlots_1.push(i); + if (slot.output) + outputSlots_1.push(i); + } + tilePrototype.getTransportSlots = function () { + return { input: inputSlots_1, output: outputSlots_1 }; + }; + } + } + else { + interface.slots = {}; + } + tilePrototype.addTransportedItem = function (obj, item, side) { + new TileEntityInterface(this).addItem(item, side); + }; + StorageInterface.data[id] = interface; + } + else { + Logger.Log("Failed to create storage interface: cannot find tile entity prototype for id " + id, "ERROR"); + } + } + StorageInterface.createInterface = createInterface; + // doesn't override native container slot (only slot object) + function addItemToSlot(item, slot, count) { + if (slot.id == 0 || slot.id == item.id && slot.data == item.data) { + var maxStack = Item.getMaxStack(item.id); + var add = Math.min(maxStack - slot.count, item.count); + if (count) + add = Math.min(add, count); + if (add > 0) { + slot.id = item.id; + slot.count += add; + slot.data = item.data; + if (item.extra) + slot.extra = item.extra; + item.count -= add; + if (item.count == 0) { + item.id = item.data = 0; + } + return add; + } + } + return 0; + } + StorageInterface.addItemToSlot = addItemToSlot; + function getStorage(region, x, y, z) { + var nativeTileEntity = region.getBlockEntity(x, y, z); + if (nativeTileEntity && nativeTileEntity.getSize() > 0) { + return new NativeContainerInterface(nativeTileEntity); + } + var tileEntity = World.getTileEntity(x, y, z, region); + if (tileEntity) { + return new TileEntityInterface(tileEntity); + } + } + StorageInterface.getStorage = getStorage; + function getNearestContainers(coords, side, excludeSide) { + var containers = {}; + if (side >= 0 && !excludeSide) { + var dir = getRelativeCoords(coords, side); + var container = World.getContainer(dir.x, dir.y, dir.z); + if (container) { + containers[side] = container; + } + } + else + for (var i = 0; i < 6; i++) { + if (excludeSide && i == side) + continue; + var dir = getRelativeCoords(coords, i); + var container = World.getContainer(dir.x, dir.y, dir.z); + if (container) { + containers[i] = container; + } + } + return containers; + } + StorageInterface.getNearestContainers = getNearestContainers; + function getNearestLiquidStorages(coords, side, excludeSide) { + var storages = {}; + if (side >= 0 && !excludeSide) { + var dir = getRelativeCoords(coords, side); + var tileEntity = World.getTileEntity(dir.x, dir.y, dir.z); + if (tileEntity && tileEntity.liquidStorage) { + storages[side] = tileEntity; + } + } + else + for (var i = 0; i < 6; i++) { + if (excludeSide && i == side) + continue; + var dir = getRelativeCoords(coords, i); + var tileEntity = World.getTileEntity(dir.x, dir.y, dir.z); + if (tileEntity && tileEntity.liquidStorage) { + storages[i] = tileEntity; + } + } + return storages; + } + StorageInterface.getNearestLiquidStorages = getNearestLiquidStorages; + function getContainerSlots(container) { + var slots = []; + if ("slots" in container) { + for (var name in slots) { + slots.push(name); + } + } + else { + var size = container.getSize(); + for (var i = 0; i < size; i++) { + slots.push(i); + } + } + return slots; + } + StorageInterface.getContainerSlots = getContainerSlots; + function putItems(items, containers) { + for (var i in items) { + var item = items[i]; + for (var side in containers) { + if (item.count == 0) + break; + var container = containers[side]; + putItemToContainer(item, container, parseInt(side)); + } + } + } + StorageInterface.putItems = putItems; + function putItemToContainer(item, container, side, maxCount) { + var storage = newInstance(container); + return storage.addItem(item, side ^ 1, maxCount); + } + StorageInterface.putItemToContainer = putItemToContainer; + function extractItemsFromContainer(inputContainer, outputContainer, side, maxCount, oneStack) { + var inputStorage = newInstance(inputContainer); + var outputStorage = newInstance(outputContainer); + var count = 0; + var slots = outputStorage.getOutputSlots(side ^ 1); + for (var _i = 0, slots_1 = slots; _i < slots_1.length; _i++) { + var name = slots_1[_i]; + var slot = outputStorage.getSlot(name); + if (slot.id > 0) { + var added = inputStorage.addItem(slot, side, maxCount - count); + if (added > 0) { + count += added; + outputStorage.setSlot(name, slot.id, slot.count, slot.data, slot.extra); + if (oneStack || count >= maxCount) { + break; + } + } + } + } + return count; + } + StorageInterface.extractItemsFromContainer = extractItemsFromContainer; + function extractLiquid(liquid, maxAmount, input, output, inputSide) { + var outputSide = inputSide ^ 1; + var outputStorage = new TileEntityInterface(output); + if (!liquid) { + liquid = outputStorage.getLiquidStored("output", outputSide); + } + if (liquid && outputStorage.canTransportLiquid(liquid, outputSide)) { + transportLiquid(liquid, maxAmount, output, input, outputSide); + } + } + StorageInterface.extractLiquid = extractLiquid; + function transportLiquid(liquid, maxAmount, output, input, outputSide) { + if (liquid) { + var inputSide = outputSide ^ 1; + var inputStorage = new TileEntityInterface(input); + var outputStorage = new TileEntityInterface(output); + if (inputStorage.canReceiveLiquid(liquid, inputSide)) { + var amount = outputStorage.getLiquid(liquid, maxAmount); + amount = inputStorage.addLiquid(liquid, amount); + outputStorage.getLiquid(liquid, -amount); + } + } + } + StorageInterface.transportLiquid = transportLiquid; + // use it in tick function of tile entity + function checkHoppers(tile) { + if (World.getThreadTime() % 8 > 0) + return; + var region = tile.blockSource; + // input + for (var side = 1; side < 6; side++) { + var dir = getRelativeCoords(tile, side); + var block = region.getBlock(dir.x, dir.y, dir.z); + if (block.id == 154 && block.data == side + Math.pow(-1, side)) { + var container = World.getContainer(dir.x, dir.y, dir.z, region); + extractItemsFromContainer(tile, container, side, 1); + } + } + // extract + if (region.getBlockId(tile.x, tile.y - 1, tile.z) == 154) { + var container = World.getContainer(tile.x, tile.y - 1, tile.z, region); + extractItemsFromContainer(container, tile.container, 0, 1); + } + } + StorageInterface.checkHoppers = checkHoppers; +})(StorageInterface || (StorageInterface = {})); +Callback.addCallback("TileEntityAdded", function (tileEntity, created) { + if (created) { // fix of TileEntity access from ItemContainer + tileEntity.container.setParent(tileEntity); + } + if (StorageInterface.data[tileEntity.blockID]) { // reverse compatibility + tileEntity.interface = new TileEntityInterface(tileEntity); + } +}); +EXPORT("StorageInterface", StorageInterface); From 26c16802ad4efe413282ec4c7a72cc4737f3ab25 Mon Sep 17 00:00:00 2001 From: Nikolay Savenko Date: Wed, 9 Dec 2020 23:48:08 +0300 Subject: [PATCH 5/6] connection pipes with StorageInterfaces --- .../engine/abstract/BCEngineTileEntity.ts | 46 +++++++++++-------- .../core/engine/creative/CreativeEngine.ts | 2 + src/dev/core/pipe/item/ItemMachines.ts | 9 +++- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/dev/core/engine/abstract/BCEngineTileEntity.ts b/src/dev/core/engine/abstract/BCEngineTileEntity.ts index 691b474..99c5773 100644 --- a/src/dev/core/engine/abstract/BCEngineTileEntity.ts +++ b/src/dev/core/engine/abstract/BCEngineTileEntity.ts @@ -2,6 +2,12 @@ /// /// /// +/** + * !WARNING + * this code adapted from JAVA source of PC mod + * this structure created not by me + * dont punch me pls + */ abstract class BCEngineTileEntity implements TileEntity.TileEntityPrototype, IHeatable, IEngine { public readonly MIN_HEAT: number = 20; public readonly IDEAL_HEAT: number = 100; @@ -18,7 +24,7 @@ abstract class BCEngineTileEntity implements TileEntity.TileEntityPrototype, IHe // How many ticks ago it gave out power, capped to 4. private lastTick: number = 0; - constructor(protected texture: EngineTexture){} + constructor(protected texture: EngineTexture) { } protected data: any = {// * it will be rewriten during runtime meta: null, // * this.orientation in PC version energy: 0, // * this.energy in PC version @@ -53,7 +59,7 @@ abstract class BCEngineTileEntity implements TileEntity.TileEntityPrototype, IHe } public setOrientation(value: number) { - if (typeof(value) == "number") { + if (typeof (value) == "number") { const { x, y, z } = this; this.blockSource.setBlock(x, y, z, this.blockSource.getBlockId(x, y, z), value); this.updateClientOrientation(); @@ -65,7 +71,7 @@ abstract class BCEngineTileEntity implements TileEntity.TileEntityPrototype, IHe this.networkData.sendChanges(); } - private setProgress(value: number){ + private setProgress(value: number) { this.data.progress = value; this.networkData.putFloat("progress", value); } @@ -74,7 +80,7 @@ abstract class BCEngineTileEntity implements TileEntity.TileEntityPrototype, IHe return this.data.progress; } - private setProgressPart(value: number){ + private setProgressPart(value: number) { this.progressPart = value; } @@ -82,7 +88,7 @@ abstract class BCEngineTileEntity implements TileEntity.TileEntityPrototype, IHe return this.progressPart; } - private setEnergyStage(value: EngineHeat){ + private setEnergyStage(value: EngineHeat) { this.energyStage = value; this.networkData.putInt("energyStageIndex", HeatOrder.indexOf(this.energyStage)); this.networkData.sendChanges(); @@ -92,7 +98,7 @@ abstract class BCEngineTileEntity implements TileEntity.TileEntityPrototype, IHe return this.isPumping; } - public setPumping(value: boolean){ + public setPumping(value: boolean) { if (this.isPumping == value) return; this.isPumping = value; this.lastTick = 0; @@ -173,24 +179,24 @@ abstract class BCEngineTileEntity implements TileEntity.TileEntityPrototype, IHe } // !TileEntity event - public init(){ + public init() { this.checkOrientation = true; } // !TileEntity event - public redstone(params){ + public redstone(params) { this.isRedstonePowered = params.signal > 0; } // !TileEntity event - public tick(){ + public tick() { if (this.checkOrientation) this.updateConnectionSide(); if (this.lastTick < 4) this.lastTick++; this.updateHeat(); this.getEnergyStage(); - if (this.getEnergyStage() === EngineHeat.OVERHEAT){ + if (this.getEnergyStage() === EngineHeat.OVERHEAT) { this.data.energy = Math.max(this.data.energy - 50, 0); return; } @@ -234,7 +240,7 @@ abstract class BCEngineTileEntity implements TileEntity.TileEntityPrototype, IHe } public click(id, count, data) { - if(id != ItemID.bc_wrench) return false; + if (id != ItemID.bc_wrench) return false; if (this.getEnergyStage() == EngineHeat.OVERHEAT) { this.setEnergyStage(this.computeEnergyStage()); } @@ -249,21 +255,21 @@ abstract class BCEngineTileEntity implements TileEntity.TileEntityPrototype, IHe // ! @MineExplorer PLEASE make EnergyTileRegistry BlockSource support // TODO move to blockSource getConnectionSide /** @param findNext - use true value if you want to rerotate engine like a wrench */ - protected getConnectionSide(findNext : boolean = false){ + protected getConnectionSide(findNext: boolean = false) { // * In common situation ends when i gets max in 5 index // * But if fhis function calling by wrench index can go beyound // * I think this code is poor, but maybe i fix it in future const orientation = this.getOrientation(); - for(let t = 0; t < 12; t++){ + for (let t = 0; t < 12; t++) { const i = t % 6; - if(findNext) { - if(orientation == t) findNext = false; + if (findNext) { + if (orientation == t) findNext = false; continue; } const relCoords = World.getRelativeCoords(this.x, this.y, this.z, i); // * ?. is new ESNext feature. Its amazing! const energyTypes = EnergyTileRegistry.accessMachineAtCoords(relCoords.x, relCoords.y, relCoords.z)?.__energyTypes; - if(energyTypes?.RF) return i; + if (energyTypes?.RF) return i; } return null; } @@ -273,7 +279,7 @@ abstract class BCEngineTileEntity implements TileEntity.TileEntityPrototype, IHe const orientation = this.getOrientation(); if (!this.isPoweredTile(this.getEnergyProvider(orientation), orientation)) { const side = this.getConnectionSide(); - if (typeof(side) == "number") { + if (typeof (side) == "number") { this.setOrientation(side); } else this.updateClientOrientation(); } else this.updateClientOrientation(); @@ -310,7 +316,7 @@ abstract class BCEngineTileEntity implements TileEntity.TileEntityPrototype, IHe private getPowerToExtract(): number { const tile = this.getEnergyProvider(this.getOrientation()); - if(!tile) return 0; + if (!tile) return 0; const oppositeSide = World.getInverseBlockSide(this.getOrientation()); @@ -327,7 +333,7 @@ abstract class BCEngineTileEntity implements TileEntity.TileEntityPrototype, IHe } public isPoweredTile(tile: any, side: number): boolean { - if(!tile) return false; + if (!tile) return false; const oppositeSide = World.getInverseBlockSide(this.getOrientation()); if (tile.isEngine) { @@ -442,7 +448,7 @@ abstract class BCEngineTileEntity implements TileEntity.TileEntityPrototype, IHe } // ? why we need it? ask PC author about it. Maybe it should be overrided in future - protected burn(): void {} + protected burn(): void { } // abstract methods public abstract isBurning(): boolean diff --git a/src/dev/core/engine/creative/CreativeEngine.ts b/src/dev/core/engine/creative/CreativeEngine.ts index 549279e..f5fe550 100644 --- a/src/dev/core/engine/creative/CreativeEngine.ts +++ b/src/dev/core/engine/creative/CreativeEngine.ts @@ -3,6 +3,8 @@ /// /// /// +// * only for engine order in creative tab +/// class CreativeEngine extends BCEngine { public get engineType(): string { diff --git a/src/dev/core/pipe/item/ItemMachines.ts b/src/dev/core/pipe/item/ItemMachines.ts index c1c7dcd..8e1319b 100644 --- a/src/dev/core/pipe/item/ItemMachines.ts +++ b/src/dev/core/pipe/item/ItemMachines.ts @@ -5,5 +5,12 @@ const transportConnector = new TransportPipeConnector(); const basicRule = transportConnector.getConnectionRules()[0]; for (const instance of ITEM_MACHINES) { - ICRender.getGroup(basicRule.name).add(instance.id, instance.data); + ICRender.getGroup(basicRule.name).add(instance.id, instance.data); +} + +// For StorageInterface containers +// @ts-ignore +for (const blockID in StorageInterface.data) { + // @ts-ignore + ICRender.getGroup(basicRule.name).add(blockID, -1); } \ No newline at end of file From f7646b04f24e778edde6a4dab998cc371ce07030 Mon Sep 17 00:00:00 2001 From: Nikolay Savenko Date: Wed, 9 Dec 2020 23:54:18 +0300 Subject: [PATCH 6/6] varsion increment --- make.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/make.json b/make.json index 2c380ef..3eb48b3 100644 --- a/make.json +++ b/make.json @@ -3,7 +3,7 @@ "info": { "name": "BuildCraft PE", "author": "Nikolay Savenko", - "version": "1.0", + "version": "1.0.1", "description": "Port of PC version of BuildCraft" }, "api": "CoreEngine" @@ -53,8 +53,8 @@ "language": "javascript" }, { - "source": "src/dev", - "target": "main.js", + "source": "src/dev", + "target": "main.js", "type": "main", "language": "typescript" },