-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CustomScreenHandlerType Add ScreenHandlerTypeRegistrable
- Loading branch information
1 parent
0654d6e
commit cec2fe6
Showing
25 changed files
with
133 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/com/mmodding/mmodding_lib/library/screenhandlers/BasicScreenHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/mmodding/mmodding_lib/library/screenhandlers/CustomScreenHandlerType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...n/java/com/mmodding/mmodding_lib/library/screenhandlers/ScreenHandlerTypeRegistrable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters