Skip to content

Commit

Permalink
Refactored container translation
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Aug 6, 2024
1 parent f2400cf commit bdaf058
Show file tree
Hide file tree
Showing 20 changed files with 448 additions and 307 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.BlockPosition;
import com.viaversion.viaversion.libs.mcstructs.text.ATextComponent;
import net.raphimc.viabedrock.protocol.data.enums.MenuType;
import net.raphimc.viabedrock.protocol.data.enums.bedrock.ContainerType;

public class ChestContainer extends Container {

public ChestContainer(final UserConnection user, final byte windowId, final ATextComponent title, final BlockPosition position, final int size) {
super(user, windowId, MenuType.CONTAINER, title, position, size);
super(user, windowId, ContainerType.CONTAINER, title, position, size, "chest", "trapped_chest");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,59 +20,91 @@
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.BlockPosition;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.libs.mcstructs.text.ATextComponent;
import net.raphimc.viabedrock.protocol.data.enums.MenuType;
import net.raphimc.viabedrock.ViaBedrock;
import net.raphimc.viabedrock.api.util.PacketFactory;
import net.raphimc.viabedrock.protocol.data.enums.bedrock.ContainerType;
import net.raphimc.viabedrock.protocol.data.enums.java.ClickType;
import net.raphimc.viabedrock.protocol.model.BedrockItem;
import net.raphimc.viabedrock.protocol.rewriter.ItemRewriter;

import java.util.Set;
import java.util.logging.Level;

public abstract class Container {

protected final UserConnection user;
protected final byte windowId;
protected final MenuType menuType;
protected final ContainerType type;
protected final ATextComponent title;
protected final BlockPosition position;
private final BedrockItem[] items;
protected BedrockItem cursorItem = BedrockItem.empty();
protected final BedrockItem[] items;
protected final Set<String> validBlockTags;

public Container(final UserConnection user, final byte windowId, final MenuType menuType, final ATextComponent title, final BlockPosition position, final int size) {
public Container(final UserConnection user, final byte windowId, final ContainerType type, final ATextComponent title, final BlockPosition position, final int size, final String... validBlockTags) {
this.user = user;
this.windowId = windowId;
this.menuType = menuType;
this.type = type;
this.title = title;
this.position = position;
this.items = BedrockItem.emptyArray(size);
this.validBlockTags = Set.of(validBlockTags);
}

protected Container(final UserConnection user, final byte windowId, final ContainerType type, final ATextComponent title, final BlockPosition position, final BedrockItem[] items, final Set<String> validBlockTags) {
this.user = user;
this.windowId = windowId;
this.type = type;
this.title = title;
this.position = position;
this.items = items;
this.validBlockTags = validBlockTags;
}

public boolean handleContainerClick(final int revision, final short slot, final byte button, final ClickType action) {
public boolean handleClick(final int revision, final short slot, final byte button, final ClickType action) {
return false;
}

public Item[] getJavaItems() {
return this.user.get(ItemRewriter.class).javaItems(this.items);
public void clearItems() {
for (int i = 0; i < this.items.length; i++) {
this.items[i] = BedrockItem.empty();
}
}

public Item getJavaCursorItem() {
return this.user.get(ItemRewriter.class).javaItem(this.cursorItem);
public Item getJavaItem(final int slot) {
return this.user.get(ItemRewriter.class).javaItem(this.getItem(slot));
}

public Item[] getJavaItems() {
return this.user.get(ItemRewriter.class).javaItems(this.items);
}

public BedrockItem getItem(final int slot) {
return this.items[slot];
}

public void setItems(final BedrockItem[] items) {
if (items.length != this.items.length) throw new IllegalArgumentException("Items length must be equal to container size");
public void setItem(final int slot, final BedrockItem item) {
final BedrockItem oldItem = this.items[slot];
this.items[slot] = item;
this.onSlotChanged(slot, oldItem, item);
}

public void setItems(final BedrockItem[] items, final PacketWrapper javaItems) {
if (items.length != this.items.length) {
ViaBedrock.getPlatform().getLogger().log(Level.WARNING, "Tried to set items for " + this.type + ", but items array length was not correct");
javaItems.cancel();
return;
}

for (int i = 0; i < items.length; i++) {
this.setItem(i, items[i]);
}
PacketFactory.writeJavaContainerSetContent(javaItems, this);
}

public void setItem(final int slot, final BedrockItem item) {
final BedrockItem oldItem = this.items[slot];
this.items[slot] = item;
this.onSlotChanged(slot, oldItem, item);
public byte javaWindowId() {
return this.windowId();
}

public int size() {
Expand All @@ -83,8 +115,8 @@ public byte windowId() {
return this.windowId;
}

public MenuType menuType() {
return this.menuType;
public ContainerType type() {
return this.type;
}

public ATextComponent title() {
Expand All @@ -95,8 +127,8 @@ public BlockPosition position() {
return this.position;
}

public BedrockItem cursorItem() {
return this.cursorItem;
public boolean isValidBlockTag(final String tag) {
return this.validBlockTags.contains(tag);
}

protected void onSlotChanged(final int slot, final BedrockItem oldItem, final BedrockItem newItem) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.libs.mcstructs.text.ATextComponent;
import net.raphimc.viabedrock.protocol.data.enums.MenuType;
import net.raphimc.viabedrock.protocol.data.enums.bedrock.ContainerType;

import java.util.function.Consumer;

Expand All @@ -29,7 +29,7 @@ public class AnvilTextInputContainer extends FakeContainer {
private final Consumer<String> onRename;

public AnvilTextInputContainer(final UserConnection user, final ATextComponent title, final Consumer<String> onRename) {
super(user, MenuType.DO_NOT_USE_ANVIL, title);
super(user, ContainerType.ANVIL, title);

this.onRename = onRename;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
package net.raphimc.viabedrock.api.model.container.fake;

import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.libs.mcstructs.text.ATextComponent;
import net.raphimc.viabedrock.api.model.container.Container;
import net.raphimc.viabedrock.api.util.PacketFactory;
import net.raphimc.viabedrock.protocol.data.enums.MenuType;
import net.raphimc.viabedrock.protocol.data.enums.bedrock.ContainerType;
import net.raphimc.viabedrock.protocol.model.BedrockItem;
import net.raphimc.viabedrock.protocol.storage.InventoryTracker;

public abstract class FakeContainer extends Container {

public FakeContainer(final UserConnection user, final MenuType menuType, final ATextComponent title) {
super(user, user.get(InventoryTracker.class).getNextFakeWindowId(), menuType, title, null, 0);
public FakeContainer(final UserConnection user, final ContainerType type, final ATextComponent title) {
super(user, user.get(InventoryTracker.class).getNextFakeWindowId(), type, title, null, 0);
}

@Override
Expand All @@ -41,14 +42,19 @@ public void setItem(final int slot, final BedrockItem item) {
throw new UnsupportedOperationException("Fake containers cannot have bedrock items");
}

@Override
public void setItems(final BedrockItem[] items, final PacketWrapper javaItems) {
throw new UnsupportedOperationException("Fake containers cannot have bedrock items");
}

public void onAnvilRename(final String name) {
}

public void onClosed() {
}

public void close() {
PacketFactory.sendJavaContainerClose(this.user, this.windowId);
PacketFactory.sendJavaContainerClose(this.user, this.javaWindowId());
this.user.get(InventoryTracker.class).markPendingClose(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import net.raphimc.viabedrock.protocol.BedrockProtocol;
import net.raphimc.viabedrock.protocol.ServerboundBedrockPackets;
import net.raphimc.viabedrock.protocol.data.ProtocolConstants;
import net.raphimc.viabedrock.protocol.data.enums.MenuType;
import net.raphimc.viabedrock.protocol.data.enums.bedrock.ContainerType;
import net.raphimc.viabedrock.protocol.data.enums.bedrock.ModalFormCancelReason;
import net.raphimc.viabedrock.protocol.data.enums.java.ClickType;
import net.raphimc.viabedrock.protocol.storage.InventoryTracker;
Expand All @@ -60,15 +60,15 @@ public class FormContainer extends FakeContainer {
private boolean sentResponse = false;

public FormContainer(final UserConnection user, final int formId, final AForm form) {
super(user, MenuType.CONTAINER, TextUtil.stringToTextComponent("Form: " + form.getTitle()));
super(user, ContainerType.CONTAINER, TextUtil.stringToTextComponent("Form: " + form.getTitle()));

this.formId = formId;
this.form = form;
this.updateFormItems();
}

@Override
public boolean handleContainerClick(final int revision, short slot, final byte button, final ClickType action) {
public boolean handleClick(final int revision, short slot, final byte button, final ClickType action) {
if (action != ClickType.PICKUP) return false;

if (this.formItems.length > SIZE && slot == SIZE - 1) {
Expand Down Expand Up @@ -194,16 +194,22 @@ public void close() {

@Override
public Item[] getJavaItems() {
final Item[] items;
if (this.formItems.length > SIZE) {
final Item[] items = new Item[SIZE];
items = new Item[SIZE];
final int begin = this.page * (SIZE - 1);
final int end = Math.min((this.page + 1) * (SIZE - 1), this.formItems.length);
System.arraycopy(this.formItems, begin, items, 0, end - begin);
for (int i = 0; i < end - begin; i++) {
items[i] = this.formItems[begin + i].copy();
}
items[SIZE - 1] = this.createItem("minecraft:arrow", "Page navigation", "§9Left click: §6Go to next page", "§9Right click: §6Go to previous page");
return items;
} else {
return this.formItems;
items = new Item[this.formItems.length];
for (int i = 0; i < this.formItems.length; i++) {
items[i] = this.formItems[i].copy();
}
}
return items;
}

private void updateFormItems() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* This file is part of ViaBedrock - https://github.com/RaphiMC/ViaBedrock
* Copyright (C) 2023-2024 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viabedrock.api.model.container.player;

import com.viaversion.viaversion.api.connection.UserConnection;
import net.raphimc.viabedrock.protocol.data.enums.bedrock.ContainerID;
import net.raphimc.viabedrock.protocol.data.enums.bedrock.ContainerType;

public class ArmorContainer extends InventorySubContainer {

public ArmorContainer(final UserConnection user) {
super(user, (byte) ContainerID.CONTAINER_ID_ARMOR.getValue(), ContainerType.ARMOR, 4);
}

}
Loading

0 comments on commit bdaf058

Please sign in to comment.