Skip to content

Commit

Permalink
Merge pull request FabricMC#7 from BoogieMonster1O1/1.8.9
Browse files Browse the repository at this point in the history
Itemgroups v0
  • Loading branch information
shedaniel authored Jul 12, 2020
2 parents 6e9dd5f + d46a14b commit 848fc38
Show file tree
Hide file tree
Showing 23 changed files with 630 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ plugins {
def ENV = System.getenv()

class Globals {
static def baseVersion = "0.2.2"
static def baseVersion = "0.2.3"
static def mcVersion = "1.8.9"
static def yarnVersion = "+build.202007090605"
}
Expand Down
7 changes: 7 additions & 0 deletions fabric-item-groups-v0/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
archivesBaseName = "fabric-item-groups-v0"
version = getSubprojectVersion(project, "0.1.0")

dependencies {
compile project(path: ':fabric-api-base', configuration: 'dev')
compile project(path: ':fabric-resource-loader-v0', configuration: 'dev')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.fabricmc.fabric;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.Identifier;

public class ItemgroupTest implements ModInitializer {
@Override
public void onInitialize() {
ItemGroup FOO = FabricItemGroupBuilder.create(new Identifier("foo:bar")).appendItems((stacks)->{
stacks.add(new ItemStack(Items.ARROW));
stacks.add(new ItemStack(Items.APPLE));
stacks.add(new ItemStack(Items.ARROW));
stacks.add(new ItemStack(Items.ARROW));
stacks.add(new ItemStack(Items.ARROW));
}).icon(()->new ItemStack(Items.ARMOR_STAND)).build();
ItemGroup BAR = FabricItemGroupBuilder.create(new Identifier("bar:foo")).appendItems((stacks)->{
stacks.add(new ItemStack(Items.CAKE));
stacks.add(new ItemStack(Items.SKULL));
stacks.add(new ItemStack(Items.FIREWORKS));
stacks.add(new ItemStack(Items.ARROW));
stacks.add(new ItemStack(Items.ARROW));
}).icon(()->new ItemStack(Items.CARROT)).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.api.client.itemgroup;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;

import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;

import net.fabricmc.fabric.impl.item.group.ItemGroupExtensions;

public class FabricItemGroupBuilder {
private final Identifier identifier;
private Supplier<ItemStack> stackSupplier = () -> new ItemStack((Item) null);
private Consumer<List<ItemStack>> stacksForDisplay;

private FabricItemGroupBuilder(Identifier identifier) {
this.identifier = identifier;
}

public static FabricItemGroupBuilder create(Identifier identifier) {
return new FabricItemGroupBuilder(identifier);
}

public FabricItemGroupBuilder icon(Supplier<ItemStack> stackSupplier) {
this.stackSupplier = stackSupplier;
return this;
}

public FabricItemGroupBuilder appendItems(Consumer<List<ItemStack>> stacksForDisplay) {
this.stacksForDisplay = stacksForDisplay;
return this;
}

public static ItemGroup build(Identifier identifier, Supplier<ItemStack> stackSupplier) {
return new FabricItemGroupBuilder(identifier).icon(stackSupplier).build();
}

public ItemGroup build() {
((ItemGroupExtensions) ItemGroup.BUILDING_BLOCKS).fabric_expandArray();
return new ItemGroup(ItemGroup.itemGroups.length - 1, String.format("%s.%s", identifier.getNamespace(), identifier.getPath())) {
@Override
public Item getIconItem() {
return stackSupplier.get().getItem();
}

@Override
public void method_8192(List<ItemStack> list) {
if (stacksForDisplay != null) {
stacksForDisplay.accept(list);
return;
}

super.method_8192(list);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.impl.item.group;

public interface CreativeInventoryScreenExtensions {
void fabric_nextPage();

void fabric_previousPage();

int fabric_currentPage();

boolean fabric_isButtonVisible(FabricCreativeGuiComponents.Type type);

boolean fabric_isButtonEnabled(FabricCreativeGuiComponents.Type type);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.impl.item.group;

import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;

import com.mojang.blaze3d.platform.GlStateManager;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.item.ItemGroup;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Identifier;

import net.fabricmc.fabric.mixin.item.group.client.MixinScreen;

public class FabricCreativeGuiComponents {
private static final Identifier BUTTON_TEX = new Identifier("fabric", "textures/gui/creative_buttons.png");
public static final Set<ItemGroup> COMMON_GROUPS = new HashSet<>();

static {
COMMON_GROUPS.add(ItemGroup.SEARCH);
COMMON_GROUPS.add(ItemGroup.INVENTORY);
}

public static class ItemGroupButtonWidget extends ButtonWidget {
CreativeInventoryScreenExtensions extensions;
CreativeInventoryScreen gui;
public Type type;

public ItemGroupButtonWidget(int id, int x, int y, Type type, CreativeInventoryScreenExtensions extensions) {
super(id, x, y, 11, 10, type.text);
this.extensions = extensions;
this.type = type;
this.gui = (CreativeInventoryScreen) extensions;
}

@Override
public void render(MinecraftClient client, int mouseX, int mouseY) {
this.focused = mouseX >= this.x && mouseY >= this.y && mouseX < this.x + this.width && mouseY < this.y + this.height;
this.visible = extensions.fabric_isButtonVisible(type);
this.active = extensions.fabric_isButtonEnabled(type);

if (this.visible) {
int u = active && this.isFocused() ? 22 : 0;
int v = active ? 0 : 10;

MinecraftClient minecraftClient = MinecraftClient.getInstance();
minecraftClient.getTextureManager().bindTexture(BUTTON_TEX);
GlStateManager.disableLighting();
GlStateManager.color4f(1F, 1F, 1F, 1F);
this.drawTexture(this.x, this.y, u + (type == Type.NEXT ? 11 : 0), v, 11, 10);

if (this.focused) {
((MixinScreen) gui).invokeRenderTooltip(new TranslatableText("fabric.gui.creativeTabPage", extensions.fabric_currentPage() + 1, ((ItemGroup.itemGroups.length - 12) / 9) + 2).asString(), mouseX, mouseY);
}
}
}

@Override
public void mouseReleased(int mouseX, int mouseY) {
super.mouseReleased(mouseX, mouseY);
}
}

public enum Type {
NEXT(">", CreativeInventoryScreenExtensions::fabric_nextPage),
PREVIOUS("<", CreativeInventoryScreenExtensions::fabric_previousPage);

String text;
public Consumer<CreativeInventoryScreenExtensions> clickConsumer;

Type(String text, Consumer<CreativeInventoryScreenExtensions> clickConsumer) {
this.text = text;
this.clickConsumer = clickConsumer;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.impl.item.group;

public interface ItemGroupExtensions {
void fabric_expandArray();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.mixin.item.group;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;

import net.minecraft.item.ItemGroup;

import net.fabricmc.fabric.impl.item.group.ItemGroupExtensions;

@Mixin(ItemGroup.class)
public class MixinItemGroup implements ItemGroupExtensions {
@Shadow
@Final
@Mutable
public static ItemGroup[] itemGroups;

@Override
public void fabric_expandArray() {
ItemGroup[] groups = itemGroups;
itemGroups = new ItemGroup[itemGroups.length + 1];

if (groups.length >= 0) {
System.arraycopy(groups, 0, itemGroups, 0, groups.length);
}
}
}
Loading

0 comments on commit 848fc38

Please sign in to comment.