Skip to content

Commit

Permalink
Merge pull request #13 from PotatoPresident/dev
Browse files Browse the repository at this point in the history
Fixed server freeze during some quick transfers
  • Loading branch information
PotatoPresident authored Feb 20, 2021
2 parents 7f8f416 + c4d149c commit 2a411f4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 75 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ minecraft_version=1.16.4
yarn_mappings=1.16.4+build.7
loader_version=0.10.8
# Mod Properties
mod_version=1.4.0
mod_version=1.4.1
maven_group=us.potatoboy
archives_base_name=InvView
# Dependencies
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/us/potatoboy/invview/ViewCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static int inv(CommandContext<ServerCommandSource> context) throws Comman
return;
} else {
NamedScreenHandlerFactory screenHandlerFactory = new SimpleNamedScreenHandlerFactory((syncId, inv, playerEntity) ->
new PlayerInventoryScreenHandler(syncId, player.inventory, requestedPlayer.inventory),
new PlayerInventoryScreenHandler(syncId, player, requestedPlayer.inventory),
requestedPlayer.getDisplayName()
);

Expand Down Expand Up @@ -107,7 +107,7 @@ public static int trinkets(CommandContext<ServerCommandSource> context) throws C
return;
} else {
player.openHandledScreen(new SimpleNamedScreenHandlerFactory((syncId, inv, player1) ->
new TrinketScreenHandler(syncId, player.inventory, requestedPlayer),
new TrinketScreenHandler(syncId, player, requestedPlayer),
requestedPlayer.getDisplayName()
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.GenericContainerScreenHandler;
import net.minecraft.item.Items;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerListener;
import net.minecraft.screen.ScreenHandlerType;
Expand All @@ -16,14 +16,15 @@
import java.util.List;

public class PlayerInventoryScreenHandler extends ScreenHandler {
private final ServerPlayerEntity player;
private final PlayerInventory viewInventory;
private final PlayerInventory playerInventory;
private final List<ScreenHandlerListener> listeners = Lists.newArrayList();

public PlayerInventoryScreenHandler(int syncId, PlayerInventory playerInventory, PlayerInventory viewInventory) {
public PlayerInventoryScreenHandler(int syncId, ServerPlayerEntity player, PlayerInventory viewInventory) {
super(ScreenHandlerType.GENERIC_9X5, syncId);
this.player = player;
this.viewInventory = viewInventory;
this.playerInventory = playerInventory;
PlayerInventory playerInventory = player.inventory;

int rows = 5;
int i = (rows - 4) * 18;
Expand Down Expand Up @@ -51,46 +52,19 @@ public boolean canUse(PlayerEntity player) {
return true;
}

@Override
public void addListener(ScreenHandlerListener listener) {
if (!listeners.contains(listener)) {
listeners.add(listener);
listener.onHandlerRegistered(this, getStacks());
sendContentUpdates();
}
}

@Override
public void sendContentUpdates() {
for (int j = 0; j < slots.size(); ++j) {
ItemStack itemStack = slots.get(j).getStack();

for (ScreenHandlerListener screenHandlerListener: listeners) {
screenHandlerListener.onSlotUpdate(this, j, itemStack.copy());
}
}
}

@Override
public ItemStack transferSlot(PlayerEntity player, int index) {
if (!(player instanceof ServerPlayerEntity))
return ItemStack.EMPTY;

this.sendContentUpdates();
return playerInventory.getCursorStack();
this.resendInventory();
return new ItemStack(Items.OAK_PLANKS);
}

@Override
public ItemStack onSlotClick(int i, int j, SlotActionType actionType, PlayerEntity playerEntity) {
if (i < 0)
return ItemStack.EMPTY;

if (i > 40 && i < 45) {
sendContentUpdates();
return playerInventory.getCursorStack();
resendInventory();
return ItemStack.EMPTY;
}

sendContentUpdates();
return super.onSlotClick(i, j, actionType, playerEntity);
}

Expand All @@ -99,4 +73,8 @@ public void close(PlayerEntity player) {
InvView.SavePlayerData((ServerPlayerEntity) viewInventory.player);
super.close(player);
}

private void resendInventory() {
player.onHandlerRegistered(this, this.getStacks());
}
}
50 changes: 13 additions & 37 deletions src/main/java/us/potatoboy/invview/gui/TrinketScreenHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
public class TrinketScreenHandler extends ScreenHandler {
private final TrinketComponent trinketComponent;
private final ServerPlayerEntity viewedPlayer;
private final PlayerInventory playerInventory;
private final ServerPlayerEntity player;
private final List<ScreenHandlerListener> listeners = Lists.newArrayList();

public TrinketScreenHandler(int syncId, PlayerInventory playerInventory, ServerPlayerEntity viewedPlayer) {
public TrinketScreenHandler(int syncId, ServerPlayerEntity player, ServerPlayerEntity viewedPlayer) {
super(ScreenHandlerType.GENERIC_9X2, syncId);
this.playerInventory = playerInventory;
PlayerInventory playerInventory = player.inventory;
trinketComponent = TrinketsApi.getTrinketComponent(viewedPlayer);
this.viewedPlayer = viewedPlayer;
this.player = player;

int rows = 2;
int i = (rows - 4) * 18;
Expand Down Expand Up @@ -55,56 +56,31 @@ public boolean canUse(PlayerEntity player) {
return true;
}

@Override
public void addListener(ScreenHandlerListener listener) {
if (!listeners.contains(listener)) {
listeners.add(listener);
listener.onHandlerRegistered(this, getStacks());
sendContentUpdates();
}
}

@Override
public void sendContentUpdates() {
for (int j = 0; j < slots.size(); ++j) {
ItemStack itemStack = slots.get(j).getStack();

for (ScreenHandlerListener screenHandlerListener: listeners) {
screenHandlerListener.onSlotUpdate(this, j, itemStack.copy());
}
}
}

@Override
public ItemStack transferSlot(PlayerEntity player, int index) {
if (!(player instanceof ServerPlayerEntity))
return ItemStack.EMPTY;

this.sendContentUpdates();
return playerInventory.getCursorStack();
this.resendInventory();
return ItemStack.EMPTY;
}

@Override
public ItemStack onSlotClick(int i, int j, SlotActionType actionType, PlayerEntity playerEntity) {
if (actionType == SlotActionType.QUICK_MOVE) return playerInventory.getCursorStack();

sendContentUpdates();

if (i < 0)
return ItemStack.EMPTY;

if (i < TrinketSlots.getSlotCount()) {
return super.onSlotClick(i, j, actionType, playerEntity);
} else if (i > 17) {
return super.onSlotClick(i, j, actionType, playerEntity);
} else {
return playerInventory.getCursorStack();
resendInventory();
return ItemStack.EMPTY;
}
}

@Override
public void close(PlayerEntity player) {
InvView.SavePlayerData(viewedPlayer);
InvView.SavePlayerData(this.viewedPlayer);
super.close(player);
}

private void resendInventory() {
player.onHandlerRegistered(this, this.getStacks());
}
}

0 comments on commit 2a411f4

Please sign in to comment.