Skip to content

Commit

Permalink
Add BasicScreenHandler
Browse files Browse the repository at this point in the history
Add CustomScreenHandlerType
Add ScreenHandlerTypeRegistrable
  • Loading branch information
FirstMegaGame4 committed Aug 3, 2023
1 parent 0654d6e commit cec2fe6
Show file tree
Hide file tree
Showing 25 changed files with 133 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void appendStacks(ItemGroup group, DefaultedList<ItemStack> stacks) {

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public EnchantmentType getType() {

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public CustomArmorItem(ArmorMaterial material, EquipmentSlot slot, Settings sett

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public CustomAxeItem(ToolMaterial material, float attackDamage, float attackSpee

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public CustomFishingRodItem(Settings settings) {

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
public void setRegistered() {
registered.set(true);
this.registered.set(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public CustomHoeItem(ToolMaterial material, int attackDamage, float attackSpeed,

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public CustomItem(Settings settings) {

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
public void setRegistered() {
registered.set(true);
this.registered.set(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ public ScreenHandler createMenu(int syncId, PlayerInventory playerInventory, Pla

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
public void setRegistered() {
registered.set(true);
this.registered.set(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public CustomMusicDiscItem(Settings settings, SoundEvent soundEvent, int compara

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public CustomPickaxeItem(ToolMaterial material, int attackDamage, float attackSp

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public CustomShovelItem(ToolMaterial material, float attackDamage, float attackS

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public CustomSpawnEggItem(EntityType<? extends MobEntity> entityType, int primar

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
public void setRegistered() {
registered.set(true);
this.registered.set(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public CustomSwordItem(ToolMaterial toolMaterial, int attackDamage, float attack

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public PointOfInterestType getType() {

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
public void setRegistered() {
registered.set(true);
this.registered.set(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public ActionResult useOnBlock(ItemUsageContext context) {

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
public void setRegistered() {
registered.set(true);
this.registered.set(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.mmodding.mmodding_lib.library.screenhandlers;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.screen.slot.Slot;
import org.jetbrains.annotations.Nullable;

public class BasicScreenHandler extends ScreenHandler {

protected final Inventory inventory;

protected BasicScreenHandler(int inventorySize, @Nullable ScreenHandlerType<?> type, int syncId, PlayerInventory playerInventory) {
super(type, syncId);
this.inventory = new SimpleInventory(inventorySize);
this.inventory.onOpen(playerInventory.player);
}

@Override
public boolean canUse(PlayerEntity player) {
return this.inventory.canPlayerUse(player);
}

@Override
public ItemStack quickTransfer(PlayerEntity player, int fromIndex) {
ItemStack itemStack = ItemStack.EMPTY;
Slot slot = this.slots.get(fromIndex);

if (slot.hasStack()) {
ItemStack slotStack = slot.getStack();
itemStack = slotStack.copy();

if (fromIndex < this.inventory.size()) {
if (!this.insertItem(slotStack, this.inventory.size(), this.slots.size(), true)) {
return ItemStack.EMPTY;
}
} else if (!this.insertItem(slotStack, 0, this.inventory.size(), false)) {
return ItemStack.EMPTY;
}

if (slotStack.isEmpty()) {
slot.setStack(ItemStack.EMPTY);
} else {
slot.markDirty();
}
}

return itemStack;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mmodding.mmodding_lib.library.screenhandlers;

import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;

import java.util.concurrent.atomic.AtomicBoolean;

public class CustomScreenHandlerType<T extends ScreenHandler> extends ScreenHandlerType<T> implements ScreenHandlerTypeRegistrable {

private final AtomicBoolean registered = new AtomicBoolean(false);

public CustomScreenHandlerType(Factory<T> factory) {
super(factory);
}

@Override
public boolean isNotRegistered() {
return !this.registered.get();
}

@Override
public void setRegistered() {
this.registered.set(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.mmodding.mmodding_lib.library.screenhandlers;

import com.mmodding.mmodding_lib.library.utils.Registrable;
import com.mmodding.mmodding_lib.library.utils.RegistrationUtils;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.util.Identifier;

public interface ScreenHandlerTypeRegistrable extends Registrable {

default void register(Identifier identifier) {
if (this instanceof ScreenHandlerType<?> screenHandlerType && this.isNotRegistered()) {
RegistrationUtils.registerScreenHandlerType(identifier, screenHandlerType);
this.setRegistered();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.potion.Potion;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.util.Identifier;
import net.minecraft.util.Pair;
import net.minecraft.util.registry.BuiltinRegistries;
Expand Down Expand Up @@ -66,6 +68,10 @@ public static <T extends Entity> void registerEntityType(Identifier identifier,
Registry.register(Registry.ENTITY_TYPE, identifier, entityType);
}

public static <T extends ScreenHandler> void registerScreenHandlerType(Identifier identifier, ScreenHandlerType<T> screenHandlerType) {
Registry.register(Registry.SCREEN_HANDLER, identifier, screenHandlerType);
}

public static void registerBiome(Identifier identifier, Biome biome) {
BuiltinRegistries.register(BuiltinRegistries.BIOME, identifier, biome);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public CustomBiome(Supplier<Biome> biome) {

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
public void setRegistered() {
registered.set(true);
this.registered.set(true);
}

public Biome getBiome() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void register(Identifier identifier) {
}

public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

public void setRegistered() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public CustomFoliagePlacer(IntProvider radius, IntProvider offset, IntProvider f

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
public void setRegistered() {
registered.set(true);
this.registered.set(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public CustomRootPlacer(IntProvider trunkOffsetY, BlockStateProvider rootProvide

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
public void setRegistered() {
registered.set(true);
this.registered.set(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public CustomTreeDecorator() {

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
public void setRegistered() {
registered.set(true);
this.registered.set(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public CustomTrunkPlacer(int baseHeight, int firstRandomHeight, int secondRandom

@Override
public boolean isNotRegistered() {
return !registered.get();
return !this.registered.get();
}

@Override
public void setRegistered() {
registered.set(true);
this.registered.set(true);
}
}

0 comments on commit cec2fe6

Please sign in to comment.