Skip to content

Commit

Permalink
Ported some stuff from 1.20.1
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Apr 18, 2024
1 parent 45b6f33 commit d337e6d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import dev.latvian.mods.kubejs.block.SoundTypeWrapper;
import dev.latvian.mods.kubejs.block.custom.BasicBlockJS;
import dev.latvian.mods.kubejs.block.custom.ButtonBlockBuilder;
import dev.latvian.mods.kubejs.block.custom.CarpetBlockBuilder;
import dev.latvian.mods.kubejs.block.custom.CropBlockBuilder;
import dev.latvian.mods.kubejs.block.custom.FallingBlockBuilder;
import dev.latvian.mods.kubejs.block.custom.FenceBlockBuilder;
Expand Down Expand Up @@ -199,6 +200,7 @@ public void init() {
RegistryInfo.BLOCK.addType("falling", FallingBlockBuilder.class, FallingBlockBuilder::new);
RegistryInfo.BLOCK.addType("crop", CropBlockBuilder.class, CropBlockBuilder::new);
RegistryInfo.BLOCK.addType("cardinal", HorizontalDirectionalBlockBuilder.class, HorizontalDirectionalBlockBuilder::new);
RegistryInfo.BLOCK.addType("carpet", CarpetBlockBuilder.class, CarpetBlockBuilder::new);

RegistryInfo.ITEM.addType("basic", BasicItemJS.Builder.class, BasicItemJS.Builder::new);
RegistryInfo.ITEM.addType("sword", SwordItemBuilder.class, SwordItemBuilder::new);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dev.latvian.mods.kubejs.block.custom;

import dev.latvian.mods.kubejs.client.VariantBlockStateGenerator;
import dev.latvian.mods.kubejs.generator.AssetJsonGenerator;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.CarpetBlock;

public class CarpetBlockBuilder extends ShapedBlockBuilder {
public CarpetBlockBuilder(ResourceLocation i) {
super(i, "_carpet");
tagBoth(BlockTags.WOOL_CARPETS.location());
}

@Override
public Block createObject() {
return new CarpetBlock(createProperties());
}

@Override
protected void generateBlockStateJson(VariantBlockStateGenerator bs) {
var mod = newID("block/", "").toString();
bs.variant("", (v) -> v.model(mod));
}

@Override
protected void generateBlockModelJsons(AssetJsonGenerator generator) {
var texture = textures.get("texture").getAsString();

generator.blockModel(id, m -> {
m.parent("minecraft:block/carpet");
m.texture("wool", texture);
});
}

public CarpetBlockBuilder texture(String texture) {
return (CarpetBlockBuilder) textureAll(texture);
}
}
28 changes: 18 additions & 10 deletions src/main/java/dev/latvian/mods/kubejs/core/ServerPlayerKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.players.UserBanListEntry;
import net.minecraft.util.Mth;
import net.minecraft.world.Container;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
Expand Down Expand Up @@ -186,20 +188,22 @@ public AbstractContainerMenu createMenu(int i, Inventory inventory, Player playe
});
}

default ItemStack[] kjs$captureInventory(boolean autoRestore) {
default Container kjs$captureInventory(boolean autoRestore) {
var playerItems = kjs$self().getInventory().items;

var captured = new ItemStack[playerItems.size()];
var captured = new SimpleContainer(playerItems.size());
var map = new HashMap<Integer, ItemStack>();

for (int i = 0; i < captured.length; i++) {
for (int i = 0; i < playerItems.size(); i++) {
var c = playerItems.set(i, ItemStack.EMPTY);

if (autoRestore && !c.isEmpty()) {
map.put(i, c);
}
if (!c.isEmpty()) {
if (autoRestore) {
map.put(i, c);
}

captured[i] = c.copy();
captured.setItem(i, c.copy());
}
}

if (autoRestore && !map.isEmpty()) {
Expand All @@ -213,12 +217,16 @@ public AbstractContainerMenu createMenu(int i, Inventory inventory, Player playe
var data = new ChestMenuData(kjs$self(), title, Mth.clamp(rows, 1, 6));
gui.accept(data);

if (kjs$self().containerMenu instanceof CustomChestMenu open && open.data.rows == data.rows && open.data.title.equals(title)) {
if (kjs$self().containerMenu instanceof CustomChestMenu open) {
data.capturedInventory = open.data.capturedInventory;
} else {
data.capturedInventory = kjs$captureInventory(true);
}

if (kjs$self().containerMenu instanceof CustomChestMenu open && open.data.rows == data.rows && open.data.title.equals(title)) {
open.data = data;
data.sync();
} else {
data.capturedInventory = data.playerSlots ? new ItemStack[0] : kjs$captureInventory(true);
data.sync();

kjs$self().openMenu(new MenuProvider() {
Expand All @@ -229,7 +237,7 @@ public Component getDisplayName() {

@Override
public AbstractContainerMenu createMenu(int i, Inventory inventory, Player player) {
return new CustomChestMenu(i, inventory, data);
return new CustomChestMenu(i, data);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Container;
import net.minecraft.world.inventory.ClickType;
import net.minecraft.world.item.ItemStack;

import java.util.function.Consumer;

public class ChestMenuData {
public final ServerPlayer player;
public final Component title;
public Component title;
public final int rows;
public final ChestMenuSlot[] slots;
public ChestMenuClickEvent.Callback anyClicked;
public ChestMenuInventoryClickEvent.Callback inventoryClicked;
public boolean playerSlots;
public Runnable closed;
public ItemStack mouseItem;
public ItemStack[] capturedInventory;
public Container capturedInventory;

public ChestMenuData(ServerPlayer player, Component title, int rows) {
this.player = player;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.latvian.mods.kubejs.gui.chest;

import dev.latvian.mods.kubejs.util.ConsoleJS;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ClickType;
Expand All @@ -23,7 +22,7 @@ public class CustomChestMenu extends AbstractContainerMenu {

public ChestMenuData data;

public CustomChestMenu(int containerId, Inventory inventory, ChestMenuData data) {
public CustomChestMenu(int containerId, ChestMenuData data) {
super(TYPES[data.rows - 1], containerId);
this.data = data;

Expand All @@ -38,12 +37,12 @@ public CustomChestMenu(int containerId, Inventory inventory, ChestMenuData data)
if (data.playerSlots) {
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 9; x++) {
addSlot(new Slot(inventory, x + y * 9 + 9, 8 + x * 18, 103 + y * 18 + k));
addSlot(new Slot(data.capturedInventory, x + y * 9 + 9, 8 + x * 18, 103 + y * 18 + k));
}
}

for (int x = 0; x < 9; x++) {
addSlot(new Slot(inventory, x, 8 + x * 18, 161 + k));
addSlot(new Slot(data.capturedInventory, x, 8 + x * 18, 161 + k));
}
} else {
for (int y = 0; y < 3; y++) {
Expand Down

0 comments on commit d337e6d

Please sign in to comment.