- 1,007
- 36
- 206
Имеются вот такие гуи, контейнер и тайл
Вопрос в названии, как из тайла получить инфу для гуи?
Java:
public class ConsoleTile extends TileEntity {
int xcoord;
int ycoord;
int zcoord;
public ConsoleTile() {
super(CONSOLE_TILE.get());
}
@Override
public void setRemoved() {
super.setRemoved();
}
@Override
public void load(BlockState state, CompoundNBT tag) {
//itemHandler.deserializeNBT(tag.getCompound("inv"));
//energyStorage.deserializeNBT(tag.getCompound("energy"));
xcoord = tag.getInt("xcoord");
ycoord = tag.getInt("ycoord");
zcoord = tag.getInt("zcoord");
System.out.println(xcoord);
System.out.println(ycoord);
System.out.println(zcoord);
super.load(state, tag);
}
@Override
public CompoundNBT save(CompoundNBT tag) {
//tag.put("inv", itemHandler.serializeNBT());
//tag.put("energy", energyStorage.serializeNBT());
tag.putInt("xcoord", xcoord);
tag.putInt("ycoord", ycoord);
tag.putInt("zcoord", zcoord);
return super.save(tag);
}
public BlockPos getCoords(){
return new BlockPos(this.xcoord, this.ycoord, this.zcoord);
}
}
Java:
public class ConsoleContainer extends Container {
public ConsoleTile tile;
public ConsoleContainer(int windowId, World world, ConsoleTile tile,
PlayerInventory playerInventory, PlayerEntity player) {
super(ModContainers.CONSOLE_CONTAINER.get(), windowId);
this.tile = tile;
System.out.println(tile.xcoord);
System.out.println(tile.ycoord);
System.out.println(tile.zcoord);
}
@Override
public boolean stillValid(PlayerEntity p_75145_1_) {
return true;
}
}
Java:
public class GuiConsole extends ContainerScreen<ConsoleContainer> {
private static final int WIDTH = 179;
private static final int HEIGHT = 151;
private char[] allowedChars = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\u0008', '\u001B', '-'
}; //0-9, backspace and escape
private int imageWidth = 176;
private int imageHeight = 166;
private int leftPos;
private int topPos;
private int xcoord;
private int ycoord;
private int zcoord;
private ConsoleContainer container;
private TextFieldWidget keycodeTextbox;
private TextFieldWidget keycodeTextbox1;
private TextFieldWidget keycodeTextbox2;
private String currentString = "";
private static final int MAX_CHARS = 20;
private ResourceLocation GUI = new ResourceLocation(Reference.MODID, "textures/gui/spawner_gui.png");
public GuiConsole(ConsoleContainer p_i51105_1_, PlayerInventory p_i51105_2_, ITextComponent p_i51105_3_) {
super(p_i51105_1_, p_i51105_2_, p_i51105_3_);
this.container = p_i51105_1_;
}
/*public GuiConsole(BlockPos pos, ConsoleTile te) {
super(new TranslationTextComponent("screen.mytutorial.spawn"));
this.pos = pos;
this.te = te;
}*/
@Override
protected void init() {
int relX = (this.width - WIDTH) / 2;
int relY = (this.height - HEIGHT) / 2;
//minecraft.keyboardHandler.setSendRepeatsToGui(true);
System.out.println(this.container.tile.getCoords().getX());
System.out.println(this.container.tile.getCoords().getY());
System.out.println(this.container.tile.getCoords().getZ());
System.out.println(this.container.tile.xcoord);
System.out.println(this.container.tile.ycoord);
System.out.println(this.container.tile.zcoord);
xcoord = this.container.tile.getCoords().getX();
ycoord = this.container.tile.getCoords().getY();
zcoord = this.container.tile.getCoords().getZ();
addButton(new Button((width / 2 + 42), height / 2 - 26, 20, 20, new StringTextComponent("Save"), this::saveButtonClicked));
addButton(keycodeTextbox = new TextFieldWidget(font, width / 2 - 37, height / 2 - 70, 77, 12, new StringTextComponent("")));
keycodeTextbox.setValue(String.valueOf(xcoord));
keycodeTextbox.setMaxLength(MAX_CHARS);
keycodeTextbox.setFilter(s -> s.matches("[-,0-9]*")); //allow any amount of numbers and any amount of asterisks
addButton(keycodeTextbox1 = new TextFieldWidget(font, width / 2 - 37, height / 2 - 50, 77, 12, new StringTextComponent("0")));
keycodeTextbox1.setValue(String.valueOf(ycoord));
keycodeTextbox1.setMaxLength(MAX_CHARS);
keycodeTextbox1.setFilter(s -> s.matches("[-,0-9]*")); //allow any amount of numbers and any amount of asterisks
addButton(keycodeTextbox2 = new TextFieldWidget(font, width / 2 - 37, height / 2 - 30, 77, 12, new StringTextComponent("0")));
keycodeTextbox2.setValue(String.valueOf(zcoord));
keycodeTextbox2.setMaxLength(MAX_CHARS);
keycodeTextbox2.setFilter(s -> s.matches("[-,0-9]*")); //allow any amount of numbers and any amount of asterisks
//setInitialFocus(keycodeTextbox);
}
/*@Override
public void removed() {
super.removed();
minecraft.keyboardHandler.setSendRepeatsToGui(false);
}*/
@Override
public void onClose(){
super.onClose();
xcoord = Integer.parseInt(keycodeTextbox.getValue());
ycoord = Integer.parseInt(keycodeTextbox1.getValue());
zcoord = Integer.parseInt(keycodeTextbox2.getValue());
}
@Override
public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
this.renderBackground(matrixStack);
this.renderBg(matrixStack, partialTicks, mouseX, mouseY);
super.render(matrixStack, mouseX, mouseY, partialTicks);
this.renderTooltip(matrixStack, mouseX, mouseY);
}
@Override
protected void renderBg(MatrixStack matrixStack, float partialTicks, int x, int y) {
RenderSystem.color4f(1f, 1f, 1f, 1f);
this.minecraft.getTextureManager().bind(GUI);
int i = this.leftPos;
int j = this.topPos;
this.blit(matrixStack, i, j, 0, 0, this.width, this.height);
}
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
return super.keyPressed(keyCode, scanCode, modifiers);
}
protected void saveButtonClicked(Button button) {
SaveCoordsMessage message = new SaveCoordsMessage(new BlockPos(Integer.parseInt(keycodeTextbox.getValue()), Integer.parseInt(keycodeTextbox1.getValue()), Integer.parseInt(keycodeTextbox2.getValue())),container.tile.getBlockPos());
NetworkHandler.sendToServer(message);
}
}