Skip to content

Commit 984cc52

Browse files
updated StorageInterface
1 parent 53d977c commit 984cc52

2 files changed

Lines changed: 67 additions & 44 deletions

File tree

declarations/lib/StorageInterface.d.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
/// <reference path="../core-engine.d.ts" />
2-
1+
/// <reference path="./core-engine.d.ts" />
32
declare type Container = NativeTileEntity | UI.Container | ItemContainer;
43
interface StorageDescriptor {
54
slots?: {
65
[key: string]: SlotData;
76
};
7+
liquidUnitRatio?: number;
88
isValidInput?(item: ItemInstance, side: number, tileEntity: TileEntity): boolean;
99
addItem?(item: ItemInstance, side?: number, maxCount?: number): number;
1010
getInputSlots?(side?: number): string[] | number[];
1111
getOutputSlots?(side?: number): string[] | number[];
12-
canReceiveLiquid?(liquid: string, side?: number): boolean;
13-
canTransportLiquid?(liquid: string, side?: number): boolean;
14-
addLiquid?(liquid: string, amount: number): number;
15-
getLiquid?(liquid: string, amount: number): number;
16-
getLiquidStored?(storageName: string): string;
17-
getLiquidStorage?(storageName: string): string;
12+
canReceiveLiquid?(liquid: string, side: number): boolean;
13+
canTransportLiquid?(liquid: string, side: number): boolean;
14+
receiveLiquid?(liquidStorage: ILiquidStorage, liquid: string, amount: number): number;
15+
extractLiquid?(liquidStorage: ILiquidStorage, liquid: string, amount: number): number;
16+
getInputTank?(side: number): ILiquidStorage;
17+
getOutputTank?(side: number): ILiquidStorage;
1818
}
1919
interface Storage extends StorageDescriptor {
2020
container: Container;
@@ -23,10 +23,10 @@ interface Storage extends StorageDescriptor {
2323
setSlot(name: string | number, id: number, count: number, data: number, extra?: ItemExtraData): void;
2424
getContainerSlots(): string[] | number[];
2525
getInputSlots(side?: number): string[] | number[];
26+
getOutputSlots(side?: number): string[] | number[];
2627
getReceivingItemCount(item: ItemInstance, side?: number): number;
2728
addItemToSlot(name: string | number, item: ItemInstance, maxCount?: number): number;
2829
addItem(item: ItemInstance, side?: number, maxCount?: number): number;
29-
getOutputSlots(side?: number): string[] | number[];
3030
clearContainer(): void;
3131
}
3232
interface SlotData {
@@ -37,6 +37,15 @@ interface SlotData {
3737
isValid?(item: ItemInstance, side: number, tileEntity: TileEntity): boolean;
3838
canOutput?(item: ItemInstance, side: number, tileEntity: TileEntity): boolean;
3939
}
40+
interface ILiquidStorage {
41+
getLiquidStored(): string;
42+
getLimit(liquid: string): number;
43+
getAmount(liquid: string): number;
44+
getLiquid(liquid: string, amount: number): number;
45+
addLiquid(liquid: string, amount: number): number;
46+
isFull(): boolean;
47+
isEmpty(): boolean;
48+
}
4049
declare class NativeContainerInterface implements Storage {
4150
readonly container: NativeTileEntity;
4251
readonly isNativeContainer = true;
@@ -52,6 +61,7 @@ declare class NativeContainerInterface implements Storage {
5261
clearContainer(): void;
5362
}
5463
declare class TileEntityInterface implements Storage {
64+
readonly liquidUnitRatio: number;
5565
readonly slots?: {
5666
[key: string]: SlotData;
5767
};
@@ -74,12 +84,12 @@ declare class TileEntityInterface implements Storage {
7484
addItem(item: ItemInstance, side?: number, maxCount?: number): number;
7585
getOutputSlots(side?: number): string[];
7686
clearContainer(): void;
77-
canReceiveLiquid(liquid: string, side?: number): boolean;
78-
canTransportLiquid(liquid: string, side?: number): boolean;
79-
addLiquid(liquid: string, amount: number): number;
80-
getLiquid(liquid: string, amount: number): number;
81-
getLiquidStored(storageName?: string): string;
82-
getLiquidStorage(storageName?: string): any;
87+
canReceiveLiquid(liquid: string, side: number): boolean;
88+
canTransportLiquid(liquid: string, side: number): boolean;
89+
receiveLiquid(liquidStorage: ILiquidStorage, liquid: string, amount: number): number;
90+
extractLiquid(liquidStorage: ILiquidStorage, liquid: string, amount: number): number;
91+
getInputTank(side: number): ILiquidStorage;
92+
getOutputTank(side: number): ILiquidStorage;
8393
}
8494
declare namespace StorageInterface {
8595
type ContainersMap = {
@@ -102,7 +112,7 @@ declare namespace StorageInterface {
102112
export function setSlotValidatePolicy(container: ItemContainer, slotName: string, func: (name: string, id: number, amount: number, data: number, extra: ItemExtraData, container: ItemContainer, playerUid: number) => boolean): void;
103113
export function setGlobalValidatePolicy(container: ItemContainer, func: (name: string, id: number, amount: number, data: number, extra: ItemExtraData, container: ItemContainer, playerUid: number) => boolean): void;
104114
/** Creates new interface instance for TileEntity or Container */
105-
export function newStorage(storage: TileEntity | Container): Storage;
115+
export function getInterface(storage: TileEntity | Container): Storage;
106116
/** Registers interface for block container */
107117
export function createInterface(id: number, descriptor: StorageDescriptor): void;
108118
/** Trasfers item to slot
@@ -163,7 +173,8 @@ declare namespace StorageInterface {
163173
* @maxAmount max amount of liquid that can be transfered
164174
* @inputStorage storage to input liquid
165175
* @outputStorage storage to extract liquid
166-
* @inputSide block side of input storage which is receiving liquid
176+
* @inputSide block side of input storage which is receiving
177+
* @returns left liquid amount
167178
*/
168179
export function extractLiquid(liquid: Nullable<string>, maxAmount: number, inputStorage: TileEntity | Storage, outputStorage: Storage, inputSide: number): number;
169180
/** Similar to StorageInterface.extractLiquid, but liquid must be specified */

src/lib/StorageInterface.js

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
LIBRARY({
22
name: "StorageInterface",
3-
version: 10,
3+
version: 12,
44
shared: true,
55
api: "CoreEngine"
66
});
@@ -97,6 +97,7 @@ var NativeContainerInterface = /** @class */ (function () {
9797
/// <reference path="Storage.ts" />
9898
var TileEntityInterface = /** @class */ (function () {
9999
function TileEntityInterface(tileEntity) {
100+
this.liquidUnitRatio = 1;
100101
this.isNativeContainer = false;
101102
this.tileEntity = tileEntity;
102103
this.container = tileEntity.container;
@@ -224,7 +225,7 @@ var TileEntityInterface = /** @class */ (function () {
224225
var slotData = this.slots[name];
225226
if (slotData.output) {
226227
var item = this.container.getSlot(name);
227-
if (item.id > 0 && this.isValidSlotSide(slotData.side, side) && (!slotData.canOutput || slotData.canOutput(item, side, this.tileEntity))) {
228+
if (this.isValidSlotSide(slotData.side, side) && (!slotData.canOutput || slotData.canOutput(item, side, this.tileEntity))) {
228229
slotNames.push(name);
229230
}
230231
}
@@ -237,26 +238,25 @@ var TileEntityInterface = /** @class */ (function () {
237238
}
238239
};
239240
TileEntityInterface.prototype.canReceiveLiquid = function (liquid, side) {
240-
return this.tileEntity.liquidStorage.getLimit(liquid) < LIQUID_STORAGE_MAX_LIMIT;
241+
return this.getInputTank(side).getLimit(liquid) < LIQUID_STORAGE_MAX_LIMIT;
241242
};
242243
TileEntityInterface.prototype.canTransportLiquid = function (liquid, side) {
243-
return this.tileEntity.liquidStorage.getLimit(liquid) < LIQUID_STORAGE_MAX_LIMIT;
244+
return true;
244245
};
245-
TileEntityInterface.prototype.addLiquid = function (liquid, amount) {
246-
var liquidStorage = this.getLiquidStorage("input");
246+
TileEntityInterface.prototype.receiveLiquid = function (liquidStorage, liquid, amount) {
247247
var storedLiquid = liquidStorage.getLiquidStored();
248248
if (!storedLiquid || storedLiquid == liquid) {
249-
return liquidStorage.addLiquid(liquid, amount);
249+
return amount - liquidStorage.addLiquid(liquid, amount / this.liquidUnitRatio) * this.liquidUnitRatio;
250250
}
251-
return amount;
251+
return 0;
252252
};
253-
TileEntityInterface.prototype.getLiquid = function (liquid, amount) {
254-
return this.getLiquidStorage("output").getLiquid(liquid, amount);
253+
TileEntityInterface.prototype.extractLiquid = function (liquidStorage, liquid, amount) {
254+
return liquidStorage.getLiquid(liquid, amount / this.liquidUnitRatio) * this.liquidUnitRatio;
255255
};
256-
TileEntityInterface.prototype.getLiquidStored = function (storageName) {
257-
return this.getLiquidStorage(storageName).getLiquidStored();
256+
TileEntityInterface.prototype.getInputTank = function (side) {
257+
return this.tileEntity.liquidStorage;
258258
};
259-
TileEntityInterface.prototype.getLiquidStorage = function (storageName) {
259+
TileEntityInterface.prototype.getOutputTank = function (side) {
260260
return this.tileEntity.liquidStorage;
261261
};
262262
return TileEntityInterface;
@@ -371,7 +371,7 @@ var StorageInterface;
371371
return new NativeContainerInterface(nativeTileEntity);
372372
}
373373
var tileEntity = World.getTileEntity(x, y, z, region);
374-
if (tileEntity && tileEntity.container) {
374+
if (tileEntity && tileEntity.container && tileEntity.__initialized) {
375375
return new TileEntityInterface(tileEntity);
376376
}
377377
return null;
@@ -380,7 +380,7 @@ var StorageInterface;
380380
/** Returns storage interface for TileEntity with liquid storage */
381381
function getLiquidStorage(region, x, y, z) {
382382
var tileEntity = World.getTileEntity(x, y, z, region);
383-
if (tileEntity && tileEntity.liquidStorage) {
383+
if (tileEntity && tileEntity.__initialized) {
384384
return new TileEntityInterface(tileEntity);
385385
}
386386
return null;
@@ -501,7 +501,7 @@ var StorageInterface;
501501
for (var _i = 0, slots_4 = slots; _i < slots_4.length; _i++) {
502502
var name = slots_4[_i];
503503
var slot = outputStorage.getSlot(name);
504-
if (slot.id > 0) {
504+
if (slot.id !== 0) {
505505
var added = inputStorage.addItem(slot, inputSide, maxCount - count);
506506
if (added > 0) {
507507
count += added;
@@ -521,18 +521,25 @@ var StorageInterface;
521521
* @maxAmount max amount of liquid that can be transfered
522522
* @inputStorage storage to input liquid
523523
* @outputStorage storage to extract liquid
524-
* @inputSide block side of input storage which is receiving liquid
524+
* @inputSide block side of input storage which is receiving
525+
* @returns left liquid amount
525526
*/
526527
function extractLiquid(liquid, maxAmount, inputStorage, outputStorage, inputSide) {
527-
var outputSide = inputSide ^ 1;
528528
if (!(inputStorage instanceof TileEntityInterface)) { // reverse compatibility
529529
inputStorage = new TileEntityInterface(inputStorage);
530530
}
531-
if (!liquid) {
532-
liquid = outputStorage.getLiquidStored("output");
533-
}
534-
if (liquid && outputStorage.canTransportLiquid(liquid, outputSide)) {
535-
return transportLiquid(liquid, maxAmount, outputStorage, inputStorage, outputSide);
531+
var outputSide = inputSide ^ 1;
532+
var inputTank = inputStorage.getInputTank(inputSide);
533+
var outputTank = outputStorage.getOutputTank(outputSide);
534+
if (!inputTank || !outputTank)
535+
return 0;
536+
if (!liquid)
537+
liquid = outputTank.getLiquidStored();
538+
if (liquid && outputStorage.canTransportLiquid(liquid, outputSide) && inputStorage.canReceiveLiquid(liquid, inputSide) && !inputTank.isFull(liquid)) {
539+
var amount = Math.min(outputTank.getAmount(liquid) * outputStorage.liquidUnitRatio, maxAmount);
540+
amount = inputStorage.receiveLiquid(inputTank, liquid, amount);
541+
outputStorage.extractLiquid(outputTank, liquid, amount);
542+
return amount;
536543
}
537544
return 0;
538545
}
@@ -542,10 +549,15 @@ var StorageInterface;
542549
if (!(outputStorage instanceof TileEntityInterface)) { // reverse compatibility
543550
outputStorage = new TileEntityInterface(outputStorage);
544551
}
545-
if (inputStorage.canReceiveLiquid(liquid, outputSide ^ 1)) {
546-
var amount = outputStorage.getLiquid(liquid, maxAmount);
547-
amount = inputStorage.addLiquid(liquid, amount);
548-
outputStorage.getLiquid(liquid, -amount);
552+
var inputSide = outputSide ^ 1;
553+
var inputTank = inputStorage.getInputTank(inputSide);
554+
var outputTank = outputStorage.getOutputTank(outputSide);
555+
if (!inputTank || !outputTank)
556+
return 0;
557+
if (inputStorage.canReceiveLiquid(liquid, inputSide) && !inputTank.isFull(liquid)) {
558+
var amount = Math.min(outputTank.getAmount(liquid) * outputStorage.liquidUnitRatio, maxAmount);
559+
amount = inputStorage.receiveLiquid(inputTank, liquid, amount);
560+
outputStorage.extractLiquid(outputTank, liquid, amount);
549561
return amount;
550562
}
551563
return 0;

0 commit comments

Comments
 (0)