Skip to content

Commit 86c514e

Browse files
committed
Add cache for InventoryTranslator for selected Containers
1 parent a7f01be commit 86c514e

3 files changed

Lines changed: 82 additions & 8 deletions

File tree

src/main/java/org/spongepowered/common/inventory/fabric/UniversalFabric.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,41 +37,46 @@
3737
@SuppressWarnings("unchecked")
3838
public interface UniversalFabric extends Fabric, InventoryBridge {
3939

40+
@SuppressWarnings("rawtypes")
41+
default InventoryTranslator fabric$translator() {
42+
return InventoryTranslators.getTranslator(this.getClass());
43+
}
44+
4045
@Override
4146
default Collection<InventoryBridge> fabric$allInventories() {
42-
return InventoryTranslators.getTranslator(this.getClass()).allInventories(this);
47+
return this.fabric$translator().allInventories(this);
4348
}
4449

4550
@Override
4651
default InventoryBridge fabric$get(int index) {
47-
return InventoryTranslators.getTranslator(this.getClass()).get(this, index);
52+
return this.fabric$translator().get(this, index);
4853
}
4954

5055
@Override
5156
default ItemStack fabric$getStack(int index) {
52-
return InventoryTranslators.getTranslator(this.getClass()).getStack(this, index);
57+
return this.fabric$translator().getStack(this, index);
5358
}
5459

5560
@Override
5661
default void fabric$setStack(int index, ItemStack stack) {
57-
InventoryTranslators.getTranslator(this.getClass()).setStack(this, index, stack);
62+
this.fabric$translator().setStack(this, index, stack);
5863
}
5964

6065
@Override default int fabric$getMaxStackSize() {
61-
return InventoryTranslators.getTranslator(this.getClass()).getMaxStackSize(this);
66+
return this.fabric$translator().getMaxStackSize(this);
6267
}
6368

6469
@Override default int fabric$getSize() {
65-
return InventoryTranslators.getTranslator(this.getClass()).getSize(this);
70+
return this.fabric$translator().getSize(this);
6671
}
6772

6873
@Override default void fabric$clear() {
69-
InventoryTranslators.getTranslator(this.getClass()).clear(this);
74+
this.fabric$translator().clear(this);
7075
this.fabric$captureContainer();
7176
}
7277

7378
@Override default void fabric$markDirty() {
74-
InventoryTranslators.getTranslator(this.getClass()).markDirty(this);
79+
this.fabric$translator().markDirty(this);
7580
this.fabric$captureContainer();
7681
}
7782
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* This file is part of Sponge, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.common.mixin.inventory.impl.world;
26+
27+
import net.minecraft.world.CompoundContainer;
28+
import net.minecraft.world.SimpleContainer;
29+
import net.minecraft.world.entity.player.Inventory;
30+
import net.minecraft.world.entity.vehicle.AbstractMinecartContainer;
31+
import net.minecraft.world.entity.vehicle.ChestBoat;
32+
import net.minecraft.world.inventory.MerchantContainer;
33+
import net.minecraft.world.inventory.ResultContainer;
34+
import net.minecraft.world.inventory.TransientCraftingContainer;
35+
import net.minecraft.world.level.block.entity.BaseContainerBlockEntity;
36+
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
37+
import org.spongepowered.asm.mixin.Mixin;
38+
import org.spongepowered.common.entity.player.SpongeUserInventory;
39+
import org.spongepowered.common.inventory.custom.CustomInventory;
40+
import org.spongepowered.common.inventory.fabric.InventoryTranslator;
41+
import org.spongepowered.common.inventory.fabric.InventoryTranslators;
42+
import org.spongepowered.common.inventory.fabric.UniversalFabric;
43+
44+
@Mixin(value = {
45+
AbstractMinecartContainer.class,
46+
ChestBoat.class,
47+
BaseContainerBlockEntity.class,
48+
CompoundContainer.class,
49+
TransientCraftingContainer.class,
50+
CustomInventory.class,
51+
Inventory.class,
52+
MerchantContainer.class,
53+
ResultContainer.class,
54+
SimpleContainer.class,
55+
SpongeUserInventory.class
56+
})
57+
public abstract class TraitMixin_UniversalFabric_Inventory implements UniversalFabric {
58+
59+
private @MonotonicNonNull InventoryTranslator<?> fabric$inventoryTranslator;
60+
61+
@Override
62+
public InventoryTranslator<?> fabric$translator() {
63+
if (this.fabric$inventoryTranslator == null) {
64+
this.fabric$inventoryTranslator = InventoryTranslators.getTranslator(this.getClass());
65+
}
66+
return this.fabric$inventoryTranslator;
67+
}
68+
}

src/mixins/resources/mixins.sponge.inventory.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"impl.server.level.ServerPlayer_Mixin_Inventory",
8080
"impl.world.ContainerMixin_Fabric_Inventory",
8181
"impl.world.TraitMixin_TrackedMenuBridge_Inventory",
82+
"impl.world.TraitMixin_UniversalFabric_Inventory",
8283
"impl.world.entity.LivingEntityMixin_EquipmentFabric_Inventory",
8384
"impl.world.entity.player.InventoryMixin_Bridge_Inventory",
8485
"impl.world.entity.player.InventoryMixin_Lens_Inventory",

0 commit comments

Comments
 (0)