Skip to content

Commit

Permalink
Clear Client Side Storages after Not Using for a While (closes #57)
Browse files Browse the repository at this point in the history
  • Loading branch information
hammy275 committed May 28, 2022
1 parent 867b140 commit bced34c
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ public List<I> getTrackedObjects() {
return infos;
}

/**
* Run when there are no infos
*/
public void noInfosTick() {

}

public void renderItem(ItemStack item, MatrixStack stack, Vector3d pos, float size, Direction facing,
AxisAlignedBB hitbox, boolean renderItemCounts) {
renderItem(item, stack, pos, size, facing, null, hitbox, renderItemCounts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,32 @@
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.World;

import java.util.Arrays;
import java.util.Objects;

public class ImmersiveAnvil extends AbstractImmersive<AnvilInfo> {

public static final ImmersiveAnvil singleton = new ImmersiveAnvil();

protected final double dist = 1d/3d;
protected int noInfosCooldown = 0;

public ImmersiveAnvil() {
super(-1); // All client side, no need to have a maximum immersive count
}

@Override
public void noInfosTick() {
super.noInfosTick();
if (noInfosCooldown >= 200) {
Arrays.fill(ClientStorage.anvilStorage, ItemStack.EMPTY);
Arrays.fill(ClientStorage.smithingStorage, ItemStack.EMPTY);
ClientStorage.anvilCost = 0;
} else {
noInfosCooldown++;
}
}

@Override
protected void initInfo(AnvilInfo info) {
setHitboxes(info);
Expand Down Expand Up @@ -154,6 +168,7 @@ public void trackObject(BlockPos pos) {
return;
}
}
this.noInfosCooldown = 0;
infos.add(new AnvilInfo(pos, ClientConstants.ticksToRenderAnvil));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.minecraft.world.World;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

Expand All @@ -30,10 +31,23 @@ public class ImmersiveCrafting extends AbstractImmersive<CraftingInfo> {
public static final ImmersiveCrafting singleton = new ImmersiveCrafting();
private final double spacing = 3d/16d;

protected int noInfosCooldown = 0;

public ImmersiveCrafting() {
super(-1);
}

@Override
public void noInfosTick() {
super.noInfosTick();
if (noInfosCooldown >= 200) {
Arrays.fill(ClientStorage.craftingStorage, ItemStack.EMPTY);
ClientStorage.craftingOutput = ItemStack.EMPTY;
} else {
noInfosCooldown++;
}
}

@Override
protected void initInfo(CraftingInfo info) {
setHitboxes(info);
Expand Down Expand Up @@ -155,6 +169,7 @@ public void trackObject(BlockPos tablePos) {
return;
}
}
this.noInfosCooldown = 0;
infos.add(new CraftingInfo(tablePos, ClientConstants.ticksToRenderCrafting));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class ImmersiveETable extends AbstractImmersive<EnchantingInfo> {

public static final ImmersiveETable singleton = new ImmersiveETable();

protected int noInfosCooldown = 0;

public ImmersiveETable() {
super(1); // Enchanting tables are special, let's only have one active
List<Float> yOffsets = new ArrayList<>();
Expand All @@ -45,6 +47,16 @@ public ImmersiveETable() {
}
}

@Override
public void noInfosTick() {
super.noInfosTick();
if (noInfosCooldown >= 200) {
ClientStorage.resetEnchs();
} else {
noInfosCooldown++;
}
}

@Override
protected void doTick(EnchantingInfo info, boolean isInVR) {
super.doTick(info, isInVR);
Expand Down Expand Up @@ -177,6 +189,7 @@ public void trackObject(BlockPos pos) {
return;
}
}
this.noInfosCooldown = 0;
infos.add(new EnchantingInfo(pos, ClientConstants.ticksToRenderETable));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ public static void removeLackingIngredientsFromTable(PlayerEntity player) {

}

public static void resetEnchs() {
weakInfo.textPreview = null;
midInfo.textPreview = null;
strongInfo.textPreview = null;
eTableItem = ItemStack.EMPTY;
eTableEnchCopy = ItemStack.EMPTY;
}

public static class ETableInfo {
public int levelsNeeded;
public ITextComponent textPreview = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,36 +159,41 @@ public void onDisconnect(ClientPlayerNetworkEvent.LoggedOutEvent event) {
}

protected <I extends AbstractImmersiveInfo> void tickInfos(AbstractImmersive<I> singleton) {
List<I> infos = singleton.getTrackedObjects();
List<I> toRemove = new LinkedList<>();
boolean hasTooManyImmersives = infos.size() > singleton.maxImmersives &&
singleton.maxImmersives > -1; // Can't have too many immersives if we want a negative amount!
int minIndex = -1;
int minTicksLeft = Integer.MAX_VALUE;
int i = 0;
for (I info : infos) {
singleton.tick(info, VRPluginVerify.clientInVR);
if (info.getTicksLeft() <= 0) {
toRemove.add(info);
}
if (hasTooManyImmersives) {
if (info.getTicksLeft() < minTicksLeft) {
minTicksLeft = info.getTicksLeft();
minIndex = i;
if (singleton.getTrackedObjects().size() == 0) {
singleton.noInfosTick(); // Run onNoInfos() function if we don't have any infos right now
} else {
List<I> infos = singleton.getTrackedObjects();
List<I> toRemove = new LinkedList<>();
boolean hasTooManyImmersives = infos.size() > singleton.maxImmersives &&
singleton.maxImmersives > -1; // Can't have too many immersives if we want a negative amount!
int minIndex = -1;
int minTicksLeft = Integer.MAX_VALUE;
int i = 0;

for (I info : infos) {
singleton.tick(info, VRPluginVerify.clientInVR);
if (info.getTicksLeft() <= 0) {
toRemove.add(info);
}
if (hasTooManyImmersives) {
if (info.getTicksLeft() < minTicksLeft) {
minTicksLeft = info.getTicksLeft();
minIndex = i;
}
}
i++;
}
i++;
}
if (minIndex > -1) {
I toRem = infos.get(minIndex);
if (!toRemove.contains(toRem)) {
toRemove.add(toRem);
if (minIndex > -1) {
I toRem = infos.get(minIndex);
if (!toRemove.contains(toRem)) {
toRemove.add(toRem);
}
}
}

for (I info : toRemove) {
singleton.onRemove(info);
infos.remove(info);
for (I info : toRemove) {
singleton.onRemove(info);
infos.remove(info);
}
}
}

Expand Down
12 changes: 2 additions & 10 deletions src/main/java/net/blf02/immersivemc/client/swap/ClientSwap.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void eTableSwap(int immersiveSlot, Hand hand, BlockPos pos) {
if (immersiveSlot == 0) {
ItemStack item = Minecraft.getInstance().player.getItemInHand(hand).copy();
if (!item.isEmpty() && !item.isEnchantable()) return;
resetEnchs();
ClientStorage.resetEnchs();
ClientStorage.eTableItem = item;
if (!item.isEmpty()) {
Map<Enchantment, Integer> fakeEnchs = new HashMap<>();
Expand All @@ -50,18 +50,10 @@ public static void eTableSwap(int immersiveSlot, Hand hand, BlockPos pos) {
if (itemSlot > -1) {
Network.INSTANCE.sendToServer(new DoETablePacket(itemSlot, hand, pos, immersiveSlot));
}
resetEnchs();
ClientStorage.resetEnchs();
}
}

protected static void resetEnchs() {
ClientStorage.weakInfo.textPreview = null;
ClientStorage.midInfo.textPreview = null;
ClientStorage.strongInfo.textPreview = null;
ClientStorage.eTableItem = ItemStack.EMPTY;
ClientStorage.eTableEnchCopy = ItemStack.EMPTY;
}

public static void anvilSwap(int slot, Hand hand, BlockPos pos) {
if (Minecraft.getInstance().player == null || Minecraft.getInstance().level == null) return;
boolean isReallyAnvil = Minecraft.getInstance().level.getBlockState(pos).getBlock() instanceof AnvilBlock;
Expand Down

0 comments on commit bced34c

Please sign in to comment.