~vpzom/retail

0bafc4394661cfe60654a560a67cac2621dc85b3 — Colin Reeder 3 months ago 65891b3
Add Creative Vending Block
M src/main/java/click/vpzom/mods/retail/RetailMod.java => src/main/java/click/vpzom/mods/retail/RetailMod.java +4 -3
@@ 33,7 33,8 @@ public class RetailMod {

	@SubscribeEvent
	public void registerTiles(final RegistryEvent.Register<TileEntityType<?>> evt) {
		VendingBlockEntity.TYPE = TileEntityType.Builder.create(VendingBlockEntity::new, VendingBlock.INSTANCE).build(null);
		VendingBlockEntity.TYPE = TileEntityType.Builder.create(VendingBlockEntity::new, VendingBlock.NORMAL, VendingBlock.CREATIVE)
			.build(null);
		VendingBlockEntity.TYPE.setRegistryName(new ResourceLocation(MODID, "vending_block"));
		evt.getRegistry().register(VendingBlockEntity.TYPE);
	}


@@ 47,11 48,11 @@ public class RetailMod {

	@SubscribeEvent
	public void registerBlocks(final RegistryEvent.Register<Block> evt) {
		evt.getRegistry().register(VendingBlock.INSTANCE);
		evt.getRegistry().registerAll(VendingBlock.NORMAL, VendingBlock.CREATIVE);
	}

	@SubscribeEvent
	public void registerItems(final RegistryEvent.Register<Item> evt) {
		evt.getRegistry().register(VendingBlock.ITEM);
		evt.getRegistry().registerAll(VendingBlock.NORMAL.ITEM, VendingBlock.CREATIVE.ITEM);
	}
}

M src/main/java/click/vpzom/mods/retail/RetailModClient.java => src/main/java/click/vpzom/mods/retail/RetailModClient.java +2 -1
@@ 11,6 11,7 @@ public class RetailModClient {

		ClientRegistry.bindTileEntityRenderer(VendingBlockEntity.TYPE, VendingBlockEntityRenderer::new);

		RenderTypeLookup.setRenderLayer(VendingBlock.INSTANCE, RenderType.getTranslucent());
		RenderTypeLookup.setRenderLayer(VendingBlock.NORMAL, RenderType.getTranslucent());
		RenderTypeLookup.setRenderLayer(VendingBlock.CREATIVE, RenderType.getTranslucent());
	}
}

M src/main/java/click/vpzom/mods/retail/VendingBlock.java => src/main/java/click/vpzom/mods/retail/VendingBlock.java +14 -5
@@ 7,6 7,7 @@ import net.minecraft.block.ContainerBlock;
import net.minecraft.block.material.Material;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;


@@ 20,14 21,22 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.fml.network.NetworkHooks;

public class VendingBlock extends ContainerBlock {
	public static VendingBlock INSTANCE = new VendingBlock();
	public static Item ITEM = new BlockItem(INSTANCE, new Item.Properties().group(ItemGroup.DECORATIONS)).setRegistryName(new ResourceLocation(RetailMod.MODID, "vending_block"));
	public static VendingBlock NORMAL = new VendingBlock(false);
	public static VendingBlock CREATIVE = new VendingBlock(true);

	private VendingBlock() {
	protected final boolean isCreative;

	public final Item ITEM = new BlockItem(this, new Item.Properties().group(ItemGroup.DECORATIONS));

	private VendingBlock(boolean isCreative) {
		super(AbstractBlock.Properties.create(Material.ROCK).hardnessAndResistance(10f, 3600000).notSolid());
		setRegistryName(new ResourceLocation(RetailMod.MODID, "vending_block"));
		this.isCreative = isCreative;

		setRegistryName(new ResourceLocation(RetailMod.MODID, isCreative ? "creative_vending_block" : "vending_block"));
		ITEM.setRegistryName(new ResourceLocation(RetailMod.MODID, isCreative ? "creative_vending_block" : "vending_block"));
	}

	@Override


@@ 45,7 54,7 @@ public class VendingBlock extends ContainerBlock {
				}

				if(((VendingBlockEntity) be).isUsableByPlayer(player)) {
					player.openContainer((VendingBlockEntity) be);
					NetworkHooks.openGui((ServerPlayerEntity) player, (VendingBlockEntity) be, buf -> buf.writeBoolean(isCreative));
				}
			}
		}

M src/main/java/click/vpzom/mods/retail/VendingBlockEntity.java => src/main/java/click/vpzom/mods/retail/VendingBlockEntity.java +29 -4
@@ 18,9 18,10 @@ import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.NonNullList;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.common.extensions.IForgeTileEntity;
import net.minecraftforge.common.util.Constants;

public class VendingBlockEntity extends TileEntity implements IInventory, INamedContainerProvider {
public class VendingBlockEntity extends TileEntity implements IInventory, INamedContainerProvider, IForgeTileEntity {
	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;


@@ 154,20 155,34 @@ public class VendingBlockEntity extends TileEntity implements IInventory, INamed
	}

	@Override
	public SUpdateTileEntityPacket getUpdatePacket() {
		CompoundNBT tag = new CompoundNBT();
	public CompoundNBT getUpdateTag() {
		CompoundNBT tag = super.getUpdateTag();

		tag.put("Price", price.write(new CompoundNBT()));
		tag.put("Good", good.write(new CompoundNBT()));
		if(owner != null) tag.putUniqueId("Owner", owner);

		return new SUpdateTileEntityPacket(getPos(), -1, tag);
		return tag;
	}

	@Override
	public SUpdateTileEntityPacket getUpdatePacket() {
		return new SUpdateTileEntityPacket(getPos(), -1, getUpdateTag());
	}

	@Override
	public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
		CompoundNBT tag = pkt.getNbtCompound();
		handleUpdateTag(tag);
	}

	@Override
	public void handleUpdateTag(BlockState state, CompoundNBT tag) {
		super.handleUpdateTag(state, tag);
		handleUpdateTag(tag);
	}

	private void handleUpdateTag(CompoundNBT tag) {
		price = ItemStack.read(tag.getCompound("Price"));
		good = ItemStack.read(tag.getCompound("Good"));
		owner = tag.contains("Owner") ? tag.getUniqueId("Owner") : null;


@@ 178,10 193,20 @@ public class VendingBlockEntity extends TileEntity implements IInventory, INamed
		getWorld().notifyBlockUpdate(getPos(), state, state, Constants.BlockFlags.BLOCK_UPDATE);
	}

	protected boolean isCreative() {
		return ((VendingBlock) world.getBlockState(pos).getBlock()).isCreative;
	}

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

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

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.inventory.container.Container;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;

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

	public VendingBlockScreen(Container handler, PlayerInventory inventory, ITextComponent title) {
	public VendingBlockScreen(VendingBlockScreenHandler handler, PlayerInventory inventory, ITextComponent title) {
		super(handler, inventory, title);
	}

	@Override
	protected void drawGuiContainerBackgroundLayer(MatrixStack matrices, float delta, int mouseX, int mouseY) {
		RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
		minecraft.getTextureManager().bindTexture(TEXTURE);
		minecraft.getTextureManager().bindTexture(container.isCreative ? TEXTURE_CREATIVE : TEXTURE);
		int x = (width - xSize) / 2;
		int y = (height - ySize) / 2;
		blit(matrices, x, y, 0, 0, xSize, ySize);

M src/main/java/click/vpzom/mods/retail/VendingBlockScreenHandler.java => src/main/java/click/vpzom/mods/retail/VendingBlockScreenHandler.java +19 -12
@@ 13,24 13,29 @@ import net.minecraft.network.PacketBuffer;
public class VendingBlockScreenHandler extends Container {
	public static ContainerType<VendingBlockScreenHandler> TYPE;

	protected boolean isCreative;

	private final IInventory inventory;

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

	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, VendingBlockEntity be) {
		this(syncID, playerInventory, be, be.isCreative());
	}

	public VendingBlockScreenHandler(int syncID, PlayerInventory playerInventory, IInventory inventory) {
	public VendingBlockScreenHandler(int syncID, PlayerInventory playerInventory, IInventory 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));
				}
			}
		}



@@ 56,7 61,6 @@ public class VendingBlockScreenHandler extends Container {
	public ItemStack slotClick(int slotIdx, int actionData, ClickType actionType, PlayerEntity player) {
		Slot slot = slotIdx < 0 ? null : this.inventorySlots.get(slotIdx);


		if (actionType == ClickType.PICKUP_ALL && slot != null) {
			// mostly copied from superclass but need to explicitly ignore ghost slots



@@ 87,7 91,7 @@ public class VendingBlockScreenHandler extends Container {
			return current;
		}

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



@@ 147,15 151,18 @@ public class VendingBlockScreenHandler extends Container {
	public ItemStack transferStackInSlot(PlayerEntity player, int index) {
		ItemStack itemStack = ItemStack.EMPTY;
		Slot slot = this.inventorySlots.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.mergeItemStack(itemStack2, VendingBlockEntity.FULL_INV_SIZE, this.inventorySlots.size(), true)) {
						return ItemStack.EMPTY;
					}
				} else if(index < VendingBlockEntity.FULL_INV_SIZE) {
				} else if(adjusted < VendingBlockEntity.FULL_INV_SIZE) {
					slot.putStack(ItemStack.EMPTY);
					return ItemStack.EMPTY;
				} else if (!this.mergeItemStack(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