~vpzom/retail

41ceb50a9fbfc38f09c3851e9d774d4c781438f6 — Colin Reeder a month ago 329c87e
Add Creative Vending Block
M src/main/java/click/vpzom/mods/retail/RetailMod.java => src/main/java/click/vpzom/mods/retail/RetailMod.java +11 -4
@@ 11,12 11,19 @@ public class RetailMod implements ModInitializer {

	@Override
	public void onInitialize() {
		Registry.register(Registry.BLOCK, new Identifier(MODID, "vending_block"), VendingBlock.INSTANCE);
		Registry.register(Registry.ITEM, new Identifier(MODID, "vending_block"), VendingBlock.ITEM);
		Registry.register(Registry.BLOCK, new Identifier(MODID, "vending_block"), VendingBlock.NORMAL);
		Registry.register(Registry.ITEM, new Identifier(MODID, "vending_block"), VendingBlock.NORMAL.ITEM);

		VendingBlockEntity.TYPE = Registry.register(Registry.BLOCK_ENTITY_TYPE, new Identifier(MODID, "vending_block"), BlockEntityType.Builder.create(VendingBlockEntity::new, VendingBlock.INSTANCE).build(null));
		Registry.register(Registry.BLOCK, new Identifier(MODID, "creative_vending_block"), VendingBlock.CREATIVE);
		Registry.register(Registry.ITEM, new Identifier(MODID, "creative_vending_block"), VendingBlock.CREATIVE.ITEM);

		VendingBlockEntity.TYPE = Registry.register(
			Registry.BLOCK_ENTITY_TYPE,
			new Identifier(MODID, "vending_block"),
			BlockEntityType.Builder.create(VendingBlockEntity::new, VendingBlock.NORMAL, VendingBlock.CREATIVE).build(null)
		);

		VendingBlockScreenHandler.TYPE =
			ScreenHandlerRegistry.registerSimple(new Identifier(MODID, "vending_block"), VendingBlockScreenHandler::new);
			ScreenHandlerRegistry.registerExtended(new Identifier(MODID, "vending_block"), VendingBlockScreenHandler::new);
	}
}

M src/main/java/click/vpzom/mods/retail/RetailModClient.java => src/main/java/click/vpzom/mods/retail/RetailModClient.java +2 -1
@@ 14,7 14,8 @@ public class RetailModClient implements ClientModInitializer {
	public void onInitializeClient() {
		ScreenRegistry.register(VendingBlockScreenHandler.TYPE, VendingBlockScreen::new);

		BlockRenderLayerMap.INSTANCE.putBlock(VendingBlock.INSTANCE, RenderLayer.getTranslucent());
		BlockRenderLayerMap.INSTANCE.putBlock(VendingBlock.NORMAL, RenderLayer.getTranslucent());
		BlockRenderLayerMap.INSTANCE.putBlock(VendingBlock.CREATIVE, RenderLayer.getTranslucent());

		BlockEntityRendererRegistry.INSTANCE.register(VendingBlockEntity.TYPE, VendingBlockEntityRenderer::new);
	}

M src/main/java/click/vpzom/mods/retail/VendingBlock.java => src/main/java/click/vpzom/mods/retail/VendingBlock.java +8 -4
@@ 21,11 21,15 @@ import net.minecraft.world.BlockView;
import net.minecraft.world.World;

public class VendingBlock extends BlockWithEntity {
	public static VendingBlock INSTANCE = new VendingBlock();
	public static BlockItem ITEM = new BlockItem(INSTANCE, new Item.Settings().group(ItemGroup.DECORATIONS));
	public static VendingBlock NORMAL = new VendingBlock(false);
	public static VendingBlock CREATIVE = new VendingBlock(true);
	public final BlockItem ITEM = new BlockItem(this, new Item.Settings().group(ItemGroup.DECORATIONS));

	private VendingBlock() {
	protected final boolean isCreative;

	private VendingBlock(boolean isCreative) {
		super(AbstractBlock.Settings.of(Material.STONE).strength(10f, 3600000).nonOpaque());
		this.isCreative = isCreative;
	}

	@Override


@@ 60,7 64,7 @@ public class VendingBlock extends BlockWithEntity {
		if(placer instanceof PlayerEntity) {
			BlockEntity be = world.getBlockEntity(pos);
			if(be instanceof VendingBlockEntity) {
				((VendingBlockEntity) be).owner = ((PlayerEntity) placer).getUuid();
				((VendingBlockEntity) be).owner = placer.getUuid();
			}
		}
	}

M src/main/java/click/vpzom/mods/retail/VendingBlockEntity.java => src/main/java/click/vpzom/mods/retail/VendingBlockEntity.java +20 -1
@@ 2,6 2,7 @@ package click.vpzom.mods.retail;

import java.util.UUID;
import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.block.entity.HopperBlockEntity;


@@ 12,13 13,15 @@ import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.Inventories;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.collection.DefaultedList;

public class VendingBlockEntity extends BlockEntity implements Inventory, NamedScreenHandlerFactory, BlockEntityClientSerializable {
public class VendingBlockEntity extends BlockEntity implements Inventory, ExtendedScreenHandlerFactory, BlockEntityClientSerializable {
	public static final int REAL_INV_SIZE = 9;
	public static final int FULL_INV_SIZE = REAL_INV_SIZE + 2;
	public static final int PRICE_SLOT = REAL_INV_SIZE;


@@ 167,10 170,21 @@ public class VendingBlockEntity extends BlockEntity implements Inventory, NamedS
		owner = tag.contains("Owner") ? tag.getUuid("Owner") : null;
	}

	protected boolean isCreative() {
		assert world != null;
		return ((VendingBlock) world.getBlockState(pos).getBlock()).isCreative;
	}

	public boolean tryExchange(PlayerEntity player) {
		ItemStack held = player.inventory.getMainHandStack();
		if(!held.isEmpty() && held.getItem() == price.getItem() && ItemStack.areTagsEqual(held, price) && held.getCount() >= price.getCount()) {
			// has payment, check whether we have the good
			if(isCreative()) {
				// creative, so ignore inventory
				held.decrement(price.getCount());
				player.inventory.offerOrDrop(world, good.copy());
				return true;
			}

			int goodCountLeft = good.getCount();
			int priceFitLeft = price.getCount();


@@ 222,4 236,9 @@ public class VendingBlockEntity extends BlockEntity implements Inventory, NamedS

		return false;
	}

	@Override
	public void writeScreenOpeningData(ServerPlayerEntity serverPlayerEntity, PacketByteBuf packetByteBuf) {
		packetByteBuf.writeBoolean(isCreative());
	}
}

M src/main/java/click/vpzom/mods/retail/VendingBlockScreen.java => src/main/java/click/vpzom/mods/retail/VendingBlockScreen.java +4 -3
@@ 8,17 8,18 @@ import net.minecraft.screen.ScreenHandler;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

public class VendingBlockScreen extends HandledScreen<ScreenHandler> {
public class VendingBlockScreen extends HandledScreen<VendingBlockScreenHandler> {
	private static final Identifier TEXTURE = new Identifier(RetailMod.MODID, "textures/gui/vending_block.png");
	private static final Identifier TEXTURE_CREATIVE = new Identifier(RetailMod.MODID, "textures/gui/creative_vending_block.png");

	public VendingBlockScreen(ScreenHandler handler, PlayerInventory inventory, Text title) {
	public VendingBlockScreen(VendingBlockScreenHandler handler, PlayerInventory inventory, Text title) {
		super(handler, inventory, title);
	}

	@Override
	protected void drawBackground(MatrixStack matrices, float delta, int mouseX, int mouseY) {
		RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
		client.getTextureManager().bindTexture(TEXTURE);
		client.getTextureManager().bindTexture(handler.isCreative ? TEXTURE_CREATIVE : TEXTURE);
		int x = (width - backgroundWidth) / 2;
		int y = (height - backgroundHeight) / 2;
		drawTexture(matrices, x, y, 0, 0, backgroundWidth, backgroundHeight);

M src/main/java/click/vpzom/mods/retail/VendingBlockScreenHandler.java => src/main/java/click/vpzom/mods/retail/VendingBlockScreenHandler.java +23 -9
@@ 4,28 4,39 @@ import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.screen.slot.Slot;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.util.math.BlockPos;

public class VendingBlockScreenHandler extends ScreenHandler {
	public static ScreenHandlerType<VendingBlockScreenHandler> TYPE;

	public final boolean isCreative;

	private final Inventory inventory;

	public VendingBlockScreenHandler(int syncID, PlayerInventory playerInventory) {
		this(syncID, playerInventory, new PartialGhostInventory(VendingBlockEntity.FULL_INV_SIZE, VendingBlockEntity.REAL_INV_SIZE));
	public VendingBlockScreenHandler(int syncID, PlayerInventory playerInventory, PacketByteBuf buf) {
		this(syncID, playerInventory, new PartialGhostInventory(VendingBlockEntity.FULL_INV_SIZE, VendingBlockEntity.REAL_INV_SIZE), buf.readBoolean());
	}

	public VendingBlockScreenHandler(int syncID, PlayerInventory playerInventory, VendingBlockEntity be) {
		this(syncID, playerInventory, be, be.isCreative());
	}

	public VendingBlockScreenHandler(int syncID, PlayerInventory playerInventory, Inventory inventory) {
	public VendingBlockScreenHandler(int syncID, PlayerInventory playerInventory, Inventory inventory, boolean isCreative) {
		super(TYPE, syncID);

		this.inventory = inventory;
		this.isCreative = isCreative;

		for(int y = 0; y < 3; y++) {
			for(int x = 0; x < 3; x++) {
				addSlot(new Slot(inventory, x + y * 3, 62 + x * 18, 17 + y * 18));
		if(!isCreative) {
			for (int y = 0; y < 3; y++) {
				for (int x = 0; x < 3; x++) {
					addSlot(new Slot(inventory, x + y * 3, 62 + x * 18, 17 + y * 18));
				}
			}
		}



@@ 82,7 93,7 @@ public class VendingBlockScreenHandler extends ScreenHandler {
			return current;
		}

		if(slotIdx < VendingBlockEntity.REAL_INV_SIZE || slot.inventory != inventory) {
		if((!isCreative && slotIdx < VendingBlockEntity.REAL_INV_SIZE) || slot.inventory != inventory) {
			return super.onSlotClick(slotIdx, actionData, actionType, player);
		}



@@ 142,15 153,18 @@ public class VendingBlockScreenHandler extends ScreenHandler {
	public ItemStack transferSlot(PlayerEntity player, int index) {
		ItemStack itemStack = ItemStack.EMPTY;
		Slot slot = this.slots.get(index);

		int adjusted = isCreative ? index - VendingBlockEntity.REAL_INV_SIZE : index;

		if (slot != null) {
			ItemStack itemStack2 = slot.getStack();
			if(!itemStack2.isEmpty()) {
				itemStack = itemStack2.copy();
				if (index < VendingBlockEntity.REAL_INV_SIZE) {
				if (adjusted < VendingBlockEntity.REAL_INV_SIZE) {
					if (!this.insertItem(itemStack2, VendingBlockEntity.FULL_INV_SIZE, this.slots.size(), true)) {
						return ItemStack.EMPTY;
					}
				} else if(index < VendingBlockEntity.FULL_INV_SIZE) {
				} else if(adjusted < VendingBlockEntity.FULL_INV_SIZE) {
					slot.setStack(ItemStack.EMPTY);
					return ItemStack.EMPTY;
				} else if (!this.insertItem(itemStack2, 0, VendingBlockEntity.REAL_INV_SIZE, false)) {

A src/main/resources/assets/retail/blockstates/creative_vending_block.json => src/main/resources/assets/retail/blockstates/creative_vending_block.json +5 -0
@@ 0,0 1,5 @@
{
	"variants": {
		"": {"model": "retail:block/creative_vending_block"}
	}
}

M src/main/resources/assets/retail/lang/en_us.json => src/main/resources/assets/retail/lang/en_us.json +2 -1
@@ 1,3 1,4 @@
{
	"block.retail.vending_block": "Vending Block"
	"block.retail.vending_block": "Vending Block",
	"block.retail.creative_vending_block": "Creative Vending Block"
}

A src/main/resources/assets/retail/models/block/creative_vending_block.json => src/main/resources/assets/retail/models/block/creative_vending_block.json +34 -0
@@ 0,0 1,34 @@
{
	"parent": "block/block",
	"textures": {
		"base": "minecraft:block/obsidian",
		"box": "retail:block/shortglass_creative",
		"top": "retail:block/smallglass_creative",
		"particle": "#base"
	},
	"elements": [
		{
			"from": [0, 0, 0],
			"to": [16, 1, 16],
			"faces": {
				"down": {"texture": "#base"},
				"up": {"texture": "#base"},
				"north": {"texture": "#base"},
				"south": {"texture": "#base"},
				"west": {"texture": "#base"},
				"east": {"texture": "#base"}
			}
		},
		{
			"from": [1, 1, 1],
			"to": [15, 16, 15],
			"faces": {
				"up": {"texture": "#top"},
				"north": {"texture": "#box"},
				"south": {"texture": "#box"},
				"west": {"texture": "#box"},
				"east": {"texture": "#box"}
			}
		}
	]
}

A src/main/resources/assets/retail/models/item/creative_vending_block.json => src/main/resources/assets/retail/models/item/creative_vending_block.json +3 -0
@@ 0,0 1,3 @@
{
	"parent": "retail:block/creative_vending_block"
}

A src/main/resources/assets/retail/textures/block/shortglass_creative.png => src/main/resources/assets/retail/textures/block/shortglass_creative.png +0 -0
A src/main/resources/assets/retail/textures/block/smallglass_creative.png => src/main/resources/assets/retail/textures/block/smallglass_creative.png +0 -0
A src/main/resources/assets/retail/textures/gui/creative_vending_block.png => src/main/resources/assets/retail/textures/gui/creative_vending_block.png +0 -0