Skip to content

Commit

Permalink
Merge pull request #7 from ELDEpendenci/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
eric2788 authored Aug 29, 2021
2 parents 1a7b9bc + f501ae1 commit 6198c96
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
env:
version: 0.1.1.1 # 你的版本名稱
version: 0.1.2 # 你的版本名稱
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
plugin_name: ELDependenci-MVC-plugin

Expand Down
4 changes: 2 additions & 2 deletions ELDependenci-MVC-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<artifactId>ELDependenci-MVC-Module</artifactId>
<groupId>org.eldependenci</groupId>
<version>0.1.1</version>
<version>0.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>ELDependenci-MVC-plugin</artifactId>
<version>${project.parent.version}.1</version>
<version>${project.parent.version}</version>

<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,15 @@ private void initReturnTypeManager(ReturnTypeManager returnTypeManager) {

private void initMethodParseManager(MethodParseManager parser) {
parser.registerParser((t, annos) -> t.equals(UISession.class), (annotations, t, e) -> session);
parser.registerParser((t, annos) -> {
parser.registerParser((t, annos) -> Arrays.stream(annos).anyMatch(a -> a.annotationType() == FromPattern.class), (annotations, t, e) -> {
FromPattern pattern = (FromPattern) Arrays.stream(annotations).filter(a -> a.annotationType() == FromPattern.class).findAny().orElseThrow(() -> new IllegalStateException("cannot find @FromPattern in List<ItemStack> parameters"));
if (t instanceof ParameterizedType) {
var parat = (ParameterizedType) t;
return parat.getActualTypeArguments()[0] == ItemStack.class && parat.getRawType() == List.class;
if (parat.getActualTypeArguments()[0] == ItemStack.class && parat.getRawType() == List.class){
return this.currentView.getEldgContext().getItems(pattern.value());
}
}
return false;
}, (annotations, t, e) -> {
FromPattern pattern = (FromPattern) Arrays.stream(annotations).filter(a -> a.annotationType() == FromPattern.class).findAny().orElseThrow(() -> new IllegalStateException("cannot find @FromPattern in List<ItemStack> parameters"));
return this.currentView.getEldgContext().getItems(pattern.value());
throw new IllegalStateException("@FromPattern 必須使用 List<ItemStack> 作為其類型");
});
//parser.registerParser((t, annos) -> t.equals(UIRequest.class), (annotations, t, e) -> eldgContext);
parser.registerParser((t, annos) -> t.equals(Player.class), (annotations, t, e) -> owner);
Expand Down Expand Up @@ -241,6 +241,28 @@ private void initMethodParseManager(MethodParseManager parser) {
LOGGER.debug("using " + toConvert + " to create instance of " + model);
return PersistDataUtils.mapToObject(toConvert, model);
});
parser.registerParser((t, annos) -> Arrays.stream(annos).anyMatch(a -> a.annotationType() == MapAttribute.class),
(annotations, type, event) -> {
MapAttribute attribute = (MapAttribute) Arrays.stream(annotations).filter(a -> a.annotationType() == MapAttribute.class).findAny().orElseThrow(() -> new IllegalStateException("cannot find MapAttribute annotation"));
var context = this.currentView.getEldgContext();
boolean isMap = false;
if (type instanceof ParameterizedType){
var parat = (ParameterizedType)type;
isMap = parat.getRawType() == Map.class && parat.getActualTypeArguments()[0] == String.class && parat.getActualTypeArguments()[1] == Object.class;
}

if (!isMap) throw new IllegalStateException("@MapAttribute 必須使用 Map<String, Object> 作為其類型");
Map<String, Object> fieldMap = context.getItems(attribute.value())
.stream()
.filter(item -> context.getAttribute(item, AttributeController.FIELD_TAG) != null)
.collect(Collectors
.toMap(
item -> context.getAttribute(item, AttributeController.FIELD_TAG),
item -> Optional.ofNullable(context.getAttribute(item, AttributeController.VALUE_TAG)).orElseThrow(() -> new IllegalStateException("The value tag of " + item.toString() + " is null."))
)
);
return PersistDataUtils.toNestedMap(fieldMap);
});
}

private ItemStack getItemByEvent(InventoryEvent e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static <T> T mapToObject(Map<String, Object> map, Class<T> beanClass) {
}
field.setAccessible(true);
Object value = map.get(field.getName());
if (value instanceof Map<?, ?>){
if (value instanceof Map<?, ?> && field.getType() != Map.class){
Map<String, Object> m = (Map<String, Object>) value;
value = mapToObject(m, field.getType());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ericlam.mc.eldgui.demo.test;

import com.ericlam.mc.eldgui.controller.MapAttribute;
import com.ericlam.mc.eldgui.controller.ModelAttribute;
import com.ericlam.mc.eldgui.controller.UIController;
import com.ericlam.mc.eldgui.event.ClickMapping;
Expand All @@ -8,6 +9,8 @@
import com.ericlam.mc.eldgui.view.BukkitView;
import org.bukkit.entity.Player;

import java.util.Map;

@UIController("test")
public class TestController {

Expand All @@ -22,8 +25,9 @@ public void beforeCreate(Player player){


@ClickMapping(view = TestView.class, pattern = 'A')
public void onClick(@ModelAttribute('Z') TestModel test, Player player){
public void onClick(@ModelAttribute('Z') TestModel test, Player player, @MapAttribute('Z') Map<String, Object> map){
player.sendMessage(test.toString());
player.sendMessage(map.toString());
}


Expand Down
2 changes: 1 addition & 1 deletion eldependenci-mvc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>ELDependenci-MVC-Module</artifactId>
<groupId>org.eldependenci</groupId>
<version>0.1.1</version>
<version>0.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.ericlam.mc.eldgui.controller;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 獲取指定 Pattern 的 所有數值,必須需要使用 {@code Map<String, Object>} 來裝載。
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface MapAttribute {

/**
*
* @return 指定 pattern
*/
char value();
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>org.eldependenci</groupId>
<artifactId>ELDependenci-MVC-Module</artifactId>
<packaging>pom</packaging>
<version>0.1.1</version>
<version>0.1.2</version>
<modules>
<module>eldependenci-mvc</module>
<module>ELDependenci-MVC-plugin</module>
Expand Down

0 comments on commit 6198c96

Please sign in to comment.